JavaScript / mongoose - Loop through an array to create array of Objects - javascript

At the moment I have a list of Team ID's which I have stored in the array teamIds. I also have a function findByKey in my Team mongoose schema which finds a Team based on its key. As you can see from the code I have to put each new Team.findByKey inside the previous one, if I put it outside of the previous findByKey it doesn't function correctly.
I'm having problems putting the Team.findByKey methods into some sort of for loop which can loop through the array teamIds and for each Team in the array. I am developing the function to create a new tournament, the idea is that you pass an array of teamIds to here, and it loops through each ID, searching for that Team and adding it to a teamsList array in order to create the tournament.
I need to put the last bit of code to create the array of Team objects into the last Team.findByKey method which is my problem. How would I fix this so that I can pass it n amount of teams and find each team and store it as an array of Teams?
This is my code:
app.get('/tournament', function(req, res){
function genMatches(nTeams) {
var matchArray = [];
while (nTeams > 1) {
nTeams = nTeams >> 1;
var matches = [];
for (var i = 0; i < nTeams; ++i) {
matches.push([]);
}
matchArray.push(matches);
}
return matchArray;
}
var teamIds = [
1364472344972,
1363173222886,
1363007586845,
1363007557484
]
var tournamentName = 'My Tournament';
Team.findByKey(teamIds[0], function(err, team1) {
if(err) {
util.log("Error occured");
}
if(!team1) {
util.log("The team does not exist");
}
Team.findByKey(teamIds[1], function(err, team2) {
if(err) {
return util.log("Error occured");
}
if(!team2) {
return util.log("The team does not exist");
}
Team.findByKey(teamIds[2], function(err, team3) {
if(err) {
return util.log("Error occured");
}
if(!team3) {
return util.log("The team does not exist");
}
Team.findByKey(teamIds[3], function(err, team4) {
if(err) {
return util.log("Error occured");
}
if(!team4) {
return util.log("The team does not exist");
}
var teamList = [
team1._id,
team2._id,
team3._id,
team4._id
];
var numRounds = Math.log(teamList.length)/Math.log(2);
var matches = genMatches(teamList.length);
for(var i = 0; i < matches[0].length; i++){
matches[0][i] = [ teamList[i+i], teamList[i+(i+1)] ]
}
var tournament = new Tournament({
name: tournamentName,
teams: teamList,
rounds: numRounds,
matches: matches
});
res.send(tournament);
});
});
});
});
});
Team schema:
'use strict';
var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validatePresenceOf = function(value){
return value && value.length;
};
var toLower = function(string){
return string.toLowerCase();
};
var getId = function(){
return new Date().getTime();
};
/**
* The Team schema. we will use timestamp as the unique key for each team
*/
var Team = new Schema({
'key' : {
unique : true,
type : Number,
default: getId
},
'name' : { type : String,
validate : [validatePresenceOf, 'Team name is required'],
index : { unique : true }
}
});
/**
* Get complete team details for all the teams
*/
Team.statics.getAll = function(cb){
var query = this.find({});
query.sort({key : -1});
return query.exec(cb);
};
Team.statics.findByKey = function(key, cb){
return this.find({'key' : key}, cb);
};
Team.statics.findByName = function(name, cb) {
return this.find({'name' : name}, cb);
};
module.exports = mongoose.model('Team', Team);

You can use the $in operator to look up your four teams in one query:
Team.find({key: {$in: teamIds}}, function (err, teams) {
...
});
If for some reason you need to call findByKey multiple times instead of collapsing it all into a single $in, look at using the async library's each method to do that more cleanly.

Related

Multiple Objects

I have a weird looking object that I would like to turn into an object with multiple objects. (what I mean by multiple objects in nested objects) The current object looks like this:
{
'test.txt': "This is a test\r\n\r\nI hope it'll work",
'testy.js': 'console.log("thonk");\r\n',
'thonk\\i swear\\egg.txt': 'am going to be happy?',
'thonk\\pls work.txt': 'dasdas'
}
And I want it to look like this:
{
"test.txt": "This is a test\r\n\r\nI hope it'll work",
"testy.js": "console.log('thonk');\r\n",
"thonk": {
"I swear": {
"egg.txt": "am going to be happy?"
},
"pls work.txt": "dasdas"
}
}
Edit:
here's what my code is (if u need it):
var fs = require("fs");
var path = require("path");
var walk = function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var i = 0;
(function next() {
var file = list[i++];
if (!file) return done(null, results);
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
var root = "test";
var data = {};
walk("test", function (err, results) {
if (err) throw err;
for (i in results) {
data[
results[i].replace(__dirname + "\\" + root + "\\", "")
] = fs.readFileSync(results[i], "utf8");
}
console.log(data);
});
This can be done by combining Object.keys() and Array.reduce() as follows:
const source = {
'test.txt': "This is a test\r\n\r\nI hope it'll work",
'testy.js': 'console.log("thonk");\r\n',
'thonk\\i swear\\egg.txt': 'am going to be happy?',
'thonk\\pls work.txt': 'dasdas'
}
const result = Object.keys(source).reduce((target, k) => {
const keys = k.split('\\');
if (keys.length == 1) {
target[k] = source[k];
} else {
const nestedObj = target[keys[0]] || {};
keys.slice(1).reduce((o, nestedKey, i) => {
const value = i < keys.length -2 ? {} : source[k];
o[nestedKey] = value;
return value;
}, nestedObj);
target[keys[0]] = nestedObj;
}
return target;
}, {});
console.log(result);
So you should create new object, then go through each element, combining items that you need and placing them to a new one.

Mongoose nested schema

I want to make a system of topics. each topic may have subtopics which are also topics.
meaning the subtopics may also have subtopics.
for example
example image
I tried to create a topic schema and add a subtopic field which will ref to topic schema too.
cant seem to get this working my schema code:
const mongoose = require('mongoose');
const TopicSchema = new mongoose.Schema({
name: {type:String,unique:true},
sub_topic:[{type:mongoose.Schema.Types.ObjectId, ref : 'Topic'}]
});
const Topic =mongoose.model('Topic', TopicSchema);
module.exports = Topic;
Also what the data im sending to the server should look like to make a new instance?
and on the server how do i save it?
i try to save like this now :
const topic = new Topic();
topic.name = req.body.name;
topic.sub_topic.name=req.body.sub_topic
and the data im sending is :(json)
{
"name":"TestMain",
"sub_topic":[{"name":"TestSub"}]
}
UPDATE : got this done using a recursive function.
function subtopicHandler(topic, sub_topic) {
Topic.find({
"name": topic.name
}, function (err, res) {
if (err) throw err
return;
})
if (sub_topic == undefined) {
let ntopic = new Topic();
ntopic.name = topic.name;
ntopic.sub_topic == undefined;
ntopic.save(function (err, result) {
if (err) console.log('saving err', err)
});
return ntopic._id;
}
let mainTopic = new Topic();
mainTopic.name = topic.name;
sub_topic.forEach(function (sub) {
mainTopic.sub_topic.push(subtopicHandler(sub, sub.sub_topic));
})
var retME;
mainTopic.save(function (err, result) {
if (err) {
console.log('saving err', err)
throw err;
}
});
return mainTopic._id;
}
Using this schema :
const TopicSchema = new mongoose.Schema({
name: {type:String,unique:true},
sub_topic:[{type:mongoose.Schema.Types.ObjectId, ref : 'Topic'}]
});
and data sent as :
{
"name":"A",
"sub_topic":[
{"name":"C","sub_topic":
[
{"name":"C1"}
,
{"name":"C2"}
]
}
,
{"name":"B"}
,
{"name":"D","sub_topic":
[
{"name":"D1"}
,
{"name":"D2"}
,
{"name":"D3"}
]
}
]
}
to the API endpoint
handled this way:
let mainTopic = new Topic();
mainTopic.name = req.body.name;
subtopicHandler(mainTopic, req.body.sub_topic);
})
If you are sending following json
const obj = {
"name":"TestMain",
"sub_topic":[{"name":"TestSub"}]
}
Then,
let mainTopic = new Topic();
let subTopic = new Topic();
// assuming for now you have only one sub-topic in array
subTopic.name = obj[0].name;
subTopinc.save(function(err,result)=>{
if(!err){
mainTopic.name = obj.name;
mainTopic.sub_topic = [result._id]
mainTopic.save(function(err,result){
console.log(result);
})
}
});
From you schema definition and the given json you can follow the above step to get the results.
Hope this will help you.
You can do this with sub docs check out the documentation.
https://mongoosejs.com/docs/subdocs.html

Fetched data from mongodb viewing using ejs trouble

I m creating a job search portal using nodejs,mongodb,and ejs view engine :
collection "filters" = 'username' 'tags'
collection "alljobs" = 'category' 'jobsdata'
In the following code I'm fetching 'tags' from "filters" collection and comparing with all 'category' in "alljobs" collection. And then all 'tags' array matched with 'category' view their respective 'jobsdata' using ejs view engine.
Problem: Code is working but in browser I can't see all matched category respective jobs only one category jobs data viewed. I am not able to understand where is the problem?
Code:
function processRequest(req,res){
var tags,data,jobsdata = [];
var len;
if(condition)
{....}
else{
var db = new Db('askhere', new Server('localhost', '27017'));
db.open(function (err, db) {
db.authenticate('', '', function (err, result) {
var url = 'mongodb://localhost:27017/askhere';
client.connect(url, function (err, db) {
var col = db.collection('filters');
col.find({username:req.session.userName}).toArray(function (err, items) { // find tags of session user from collection filters
console.log('items: ' + JSON.stringify(items));
items.forEach(function (doc) {
tags = doc.tags; //save tags
});
var col = db.collection('alljobs'); //find all categories jobs matched with tags data in collection alljobs
for(var i=0; i<tags.length;i++){
col.find({category:tags[i]}).toArray(function (err, items1) {
if (items1 == false) {
res.render('mainqa',{uname:req.session.userName,tags:'No Tags Selected',jobsdata:'No Tags Matched !!!',len:0});
}
items1.forEach(function (doc1) {
jobsdata = doc1.jobsdata;
var html = ["url : ", "Posted Date : ", "Job Title : ", "Company : ", "Location : "]
for (var i = 0; i < 25; i++) {
for (var j = 0; j < 5; j++) {
data.push(html[j] + jobsdata[i][j]);
} //Nested for loop
} //for loop covert 2D array in 1D
res.render('mainqa',{uname:req.session.userName,tags:tags,jobsdata:data,len:len});
}); //forEach
}); //col.find collection=alljobs
} //for loop
}); //col.find collection=filters
}); //client connect
}); //db.authenticate
}); //db.open
} //else end
} //function processRequest end
You should stop using callbacks for nesting code of multiple methods unless you want to end up with the Callback Hell. Use the async module to simplify this task, in particular you'd need to use async.waterfall() since you want to run multiple tasks that depend on each other.
The async.waterfall() method allows you to pass values between the functions in a trickle-down manner. It is quite handy when you need the results of a previous function to perform an operation with the next function in the series.
Consider restructuring following this example:
var async = require("async"),
userName = req.session.userName,
locals = {},
url = 'mongodb://localhost:27017/askhere',
db = new Db('askhere', new Server('localhost', '27017'));
async.waterfall(
[
// Open db
function(callback) {
db.open(function (err, db) {
if (err) return callback(err);
callback(null, db);
});
},
// Authenticate and connect
function(db, callback) {
db.authenticate('', '', function (err, result) {
if (err) return callback(err);
client.connect(url, function (err, res) {
if (err) return callback(err);
locals.db = db;
callback(null, locals);
});
});
},
// Query filters collection for tags of session user
function(arg, callback) {
var collection = arg.db.collection("filters");
collection.find({"username": userName}).toArray(function (err, result) {
if (err) return callback(err);
locals.tags = result.map(function(item){ return item.tags; });
callback(null, locals);
});
},
// Query alljobs collection for categories jobs matched with tags data
function(arg, callback) {
var collection = arg.db.collection("alljobs");
collection.find({"category": {"$in": arg.tags}}).toArray(function (err, result) {
if (err) return callback(err);
locals.jobsdata = result.map(function (doc){ return doc.jobsdata; });
callback(null, locals);
});
}
], function(err, result) {
if (err) { /* handle err */ };
res.render("mainqa", {
"uname": userName,
"tags": result.tags,
"jobsdata": result.jobsdata
});
});
);

Mongoose returns Mongo object, but can't access property on it

In my simple Node/Mongo/Mongoose setup, I have a function that calls the server to see what the highest ID I'm using currently is, and return the next ID. This function takes as a callback the functionality that creates a new Game.
Weird: The logger.log as it appears below outputs
result { _id: 555d83d5bb0d4e3c352d896f, gameId: 'NaN' }
but when I change the logger to
logger.log("result", result.gameId);
the output is
result { _id: 555d83d5bb0d4e3c352d896f, gameId: 'NaN' }
which makes no sense. Clearly that property is there!
Here is my code
var createGame = function(gameNickname, callback){
nextGameId(function(nextId){
var newgame = new models.Game({
"gameId": Number(nextId),
"gameNickname": gameNickname
});
newgame.save(function(result, game){
callback(result + nextId);
});
});
};
var nextGameId = function(callback){
var games = models.Game.find({}, {gameId: 1});
games.sort('-gameId').limit(1) //get the highest number roundId and add 1 to it
.exec(function (err, result) {
if (err) logger.log(err);
if (result === null){
callback(0);
}
else{
logger.log("result", result);
callback(result.gameId);
}
});
};
i recommend you use autoincrement mongoose plugin, somthing like this
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var connection = mongoose.createConnection("mongodb://localhost/db");
autoIncrement.initialize(connection);
var GameSchema = {
"gameId": {type: Number},
"gameNickname": {type: String}
}
GameSchema.plugin(autoIncrement.plugin, { model: 'Game', field: 'gameId' });
mongoose.model('Game', GameSchema);
after this you can save your game with autoinc, for example:
var Game = mongoose.model('Game');
function createNewGame(nickname){
return new Game({gameNickname: nickname}).save(function(err, res){
console.log(res);
//some code...
})
}
after execute this code you should have somnthing like this:
{
_id: "555d83d5bb0d4e3c352d896f",
gameNickname: "nickname",
gameId: 1
}

Mongoose findOne in stream callback not executed

When I execute a findOne query to a collection that is different than that of the surrounding stream, it doesn't really execute the callback (does 1 or 2). Here is my code:
schema.js:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var AutocompleteSchema = new Schema({
nGram: String,
locations: [{ type: Schema.Types.ObjectId, ref: 'Location' }]
});
module.exports = mongoose.model('Autocomplete', AutocompleteSchema);
ingest.js:
var Autocomplete = require('./schema');
var nGramAPI = require('ngram');
var cache = [];
function storeNGram(input, location) {
if(cache.indexOf(input) === -1) {
cache.push(input);
Autocomplete
.findOne({ nGram: input })
.populate('locations')
.exec(function (err, nGram) {
console.log(nGram);
if(!nGram) {
var newAutocomplete = {
nGram: input,
locations: [location._id]
};
Autocomplete.create(newAutocomplete, function(err, created) {
cache.splice(cache.indexOf(input), 1);
});
}
else {
nGram.locations.push(location._id);
sortLocations(nGram);
location.save(function(err, saved){
cache.splice(cache.indexOf(input), 1);
});
}
});
}
else {
setTimeout(function() {
storeNGram(input, location);
}, 100);
}
}
exports.ingest = function() {
console.log("Building nGrams");
var stream = Location.find().stream();
stream.on('data', function (location) {
var length = location.dispName.length > 20 ? 20 : location.dispName.length;
for(var i = 1; i <= length; i++) {
_.each(nGramAPI(i)(location.dispName), function(nGram) {
storeNGram(nGram, location);
});
}
});
}
When I execute the Autocomplete.findOne on its own outside of the stream it queries properly, but when inside it fails. Any suggestions?

Categories

Resources