strange Mongoose behaviour - document will not save into DB - node.js

In the following code, I try to populate my dev DB with some test data. I would like to first delete all documents and then add new test ones:
var mongoose = require('mongoose')
, Plan = mongoose.model('Plan')
, Async = require('async')
Async.series([
function(callback){
// delete all records
Plan.find(function(err,docs){
for (d in docs)
Plan.remove(d, function(err) {
if (err) console.log("error removing records " + err)
});
});
callback();
},
function(callback){
var planArray = [
{title: 'Plan A', body: 'Restaurant Financial Plan'},
{title: 'Plan B', body: 'Coffeeshop Financial Plan'},
{title: 'Plan C', body: 'bar Financial Plan'}
]
var arrayLength = planArray.length;
for (var i = 0; i < arrayLength; i++) {
var p = new Plan(planArray[i])
p.save(function(err, saved){
if (err)
{console.log("error creating fixture " + err)}
else {
console.log(saved)
}
})
}
callback();
}
])
The interesting (strange) behaviour is this:
- the code runs and removes all documents but does not add the new test ones.
- no errors on the console, the console.log(saved) prints each new document to the console successfully.
- if I remove the first Async function (delete all records) - then the new docs are saved into the DB.
a mongoose quirk or my misunderstanding of async flow..?

There were a few problems. Firstly you have a for loop that is firing of async removes, but these were likely not completing before your first callback was called. Better to use Async.each instead.
Also there seems to be some function naming collision happening. So for a complete example of this see the following:
var mongoose = require('mongoose'),
Async = require('async'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var planSchema = new Schema({
title: String,
body: String
});
var Plan = mongoose.model('Plan',planSchema);
Async.series([
function(call1) {
Plan.find(function(err,docs) {
if (err)
throw err;
if ( docs.length > 0 ) {
Async.each( docs, function(d, call2) {
Plan.remove(d, function(err) {
if (err)
throw err;
console.log( "deleting: " + d );
call2();
});
});
}
});
call1();
},
function(call3) {
var planArray = [
{ title: 'Plan A', body: 'Plan A' },
{ title: 'Plan B', body: 'Plan B' }
];
var arrayLength = planArray.length;
for ( var i = 0; i < arrayLength; i++ ) {
var p = new Plan(planArray[i]);
p.save(function(err,saved) {
if (err)
throw err;
console.log( "saving: " + saved );
});
}
call3();
}
]);

My guess is the latter--a misunderstanding of async flow. Your callback on the first function is invoked before you finish finding and removing documents. So while you're still finding and removing them, you are already adding some more in the second function--but these will be found and removed by the first.
You need to call the first callback only after deleting all documents. Try putting an async.each within your Plan.find callback:
Async.series([
function(callback){
// delete all records
Plan.find(function(err, docs){
Async.each(
docs, // your array
function removeDoc(d, cb) { // iterator function
Plan.remove(d, function (err) {
if (err) console.log("error removing records " + err);
return cb();
});
},
callback // executed after the iterator is done
);
},
...
Incidentally, I believe Plan.remove({}, function(err){...}) deletes all documents--no need to iterate over each document unless of course you're doing something else.

Second function starts execution after you call callback(), i. e. before find and remove calls. You have to wait until find and remove are done and the call callback().
Have a look at queue method from async: https://github.com/caolan/async#queue

Related

Foreach loop with mongoDB call / node.js

result.forEach(element => {
//Get each element
console.log("LOOP");
dbo.collection("users").findOne({email: emailGiven, "friends.email": element.email},function(errT, resultT) {
if (errT){
console.log("Query Error Inside!");
res.status(errT.status); // or use err.statusCode instead
console.log(errT);
//db.close();
//return res.send(errT.message);
}
else {
if (resultT) {
var oneUser = {
email: element.email,
username: element.username,
fullName: element.fullName,
status: resultT
};
//console.log(resultT);
foundUsers.push(oneUser);
} else {
//Not found means not added or pending
var oneUser = {
email: element.email,
username: element.username,
fullName: element.fullName,
status: 0
};
foundUsers.push(oneUser);
//console.log(emailGiven + " " + element.email)
console.log(oneUser);
}
}
});
});
i have an object array for each elemant i would like to do mongoDB call for each element and depending on the results i wanna push the results in an array as im doing, the problem is that mongoDb is async so my main thread finished before i can push results to the array foundUsers, how may i fix this issue?
As you said, need to do handle an asynchronous operation into a synchronous loop. For doing this, you can use async library. It is so useful in such operatinos.
Just install async module in your project first
npm install --save async
Afterwards, you can do sth like this:
// for use with Node-style callbacks...
var async = require("async");
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
var configs = {};
async.forEachOf(obj, (value, key, callback) => {
fs.readFile(__dirname + value, "utf8", (err, data) => {
if (err) return callback(err);
try {
configs[key] = JSON.parse(data);
} catch (e) {
return callback(e);
}
callback();
});
}, err => {
if (err) console.error(err.message);
// configs is now a map of JSON data
doSomethingWith(configs);
});
For working with this library, it uses async.forEachOf function instead of simple forEach loop. Three parameters is sent to this function.
The 1st parameter that is passed to async.forEachOf is an array to iterate over it (obj).
The 2nd parameter is a callback function that apply over each item in obj.
The 3rd or the last parameter that is passed to async.forEachOf function, is another callback function too. It is called when iteration process over every item in obj has finished.

Assign keystonejs callback function data to array

I'm new to node.js and currently working on a project using keystonejs cms and MongoDB. Now I'm stuck in getting data related to multiple collections. Because of this callback functions, I couldn't return an array with relational data. My code something similar to this sample code.
var getAgenda = function(id, callback){
callback = callback || function(){};
if(id){
AgendaDay.model.find({summit:id}).exec(function (err, results3) {
var arr_agenda = [];
var arr_agenda_item = [];
for(var key3 in results3){
AgendaItem.model.find({agendaDay:results3[key3]._id}).exec(function (err, results2){
for(var key2 in results2){
arr_agenda_item.push(
{
item_id: results2[key2]._id,
item_name: results2[key2].name,
from_time: results2[key2].time_from,
to_time: results2[key2].time_to,
desc: results2[key2].description,
fatured: results2[key2].featured,
}
);
}
arr_agenda.push(
{
name: results3[key3].name,
date: results3[key3].date,
description: results3[key3].description,
item_list:arr_agenda_item
}
);
return callback(arr_agenda);
});
}
});
}
}
exports.list = function (req, res) {
var mainarray = [];
Summit.model.find().exec(function (err, resultssummit) {
if (err) return res.json({ err: err });
if (!resultssummit) return res.json('not found');
Guest.model.find().exec(function (err, resultsguset) {
for(var key in resultssummit){
var agen_arr = [];
for(var i=0; i<resultssummit[key].guests.length; i++){
var sumid = resultssummit[key]._id;
//this is the function im trying get data and assign to mainarray
getAgenda(sumid, function(arr_agenda){
agen_arr = arr_agenda;
});
mainarray.push(
{
id: resultssummit[key]._id,
name: resultssummit[key].name,
agenda_data: agen_arr,
}
);
}
res.json({
summit: mainarray,
});
}
});
}
}
If anyone can help me out, that would be really great :)
You need to restructure this whole thing. You should not be calling mongo queries in a for loop and expecting their output at the end of the loop. Also, your response is in a for loop. That won't work.
I'll tell you how to do it. I cannot refactor all of that code for you.
Instead of putting mongodb queries in a for loop, you need to convert it in a single query. Just put the _ids in a single array and fire a single query.
AgendaItem.model.find({agendaDay:{$in:ARRAY_OF_IDS}})
You need to do the same thing for AgendaDay.model.find({summit:id}) as well.

how can run mongoose query in forEach loop

can anyone help me for how can run mongoose query in forEach loop in nodejs and suggest for inner join result need of both collections
like below details
userSchema.find({}, function(err, users) {
if (err) throw err;
users.forEach(function(u,i){
var users = [];
jobSchema.find({u_sno:s.u.sno}, function(err, j) {
if (err) throw err;
if (!u) {
res.end(JSON.stringify({
status: 'failed:Auction not found.',
error_code: '404'
}));
console.log("User not found.");
return
}
users.push(j);
})
})
res.send(JSON.stringify({status:"success",message:"successfully done",data:{jobs:j,users:u}}));
})
Schema.find() is an async function. So your last line of code will execute while you wait for the first job search is executed in your loop. I suggest change it to Promises and use Promise.all(array).
To do so, first you have to change to use Promise with mongoose. you can do this with bluebird like this:
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
Then you can use Promises instead of callbacks like this:
userSchema.find({}).then(function(users) {
var jobQueries = [];
users.forEach(function(u) {
jobQueries.push(jobSchema.find({u_sno:s.u.sno}));
});
return Promise.all(jobQueries );
}).then(function(listOfJobs) {
res.send(listOfJobs);
}).catch(function(error) {
res.status(500).send('one of the queries failed', error);
});
EDIT How to list both jobs and users
If you want to have a structure like:
[{
user: { /* user object */,
jobs: [ /* jobs */ ]
}]
you could merge the lists together. listOfJobs is in the same order as the jobQueries list, so they are in the same order as the users. Save users to a shared scope to get access to the list in the 'then function' and then merge.
..
}).then(function(listOfJobs) {
var results = [];
for (var i = 0; i < listOfJobs.length; i++) {
results.push({
user: users[i],
jobs: listOfJobs[i]
});
}
res.send(results);
}).catch(function(error) {
res.status(500).send('one of the queries failed', error);
});
A nice elegant solution is to use the cursor.eachAsync() function. Credit to https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js.
The eachAsync() function executes a (potentially async) function for
each document that the cursor returns. If that function returns a
promise, it will wait for that promise to resolve before getting the
next document. This is the easiest way to exhaust a cursor in
mongoose.
// A cursor has a `.next()` function that returns a promise. The promise
// will resolve to the next doc if there is one, or null if they are no
// more results.
const cursor = MyModel.find().sort({name: 1 }).cursor();
let count = 0;
console.log(new Date());
await cursor.eachAsync(async function(doc) {
// Wait 1 second before printing first doc, and 0.5 before printing 2nd
await new Promise(resolve => setTimeout(() => resolve(), 1000 - 500 * (count++)));
console.log(new Date(), doc);
});
No need to use forEach() which is synchronous and being called in an asynchronous fashion, that will give you wrong results.
You can use the aggregation framework and use $lookup which performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing.
So the same query can be done using a single aggregation pipeline as:
userSchema.aggregate([
{
"$lookup": {
"from": "jobs", /* underlying collection for jobSchema */
"localField": "sno",
"foreignField": "u_sno",
"as": "jobs"
}
}
]).exec(function(err, docs){
if (err) throw err;
res.send(
JSON.stringify({
status: "success",
message: "successfully done",
data: docs
})
);
})
You can use this:
db.collection.find(query).forEach(function(err, doc) {
// ...
});

async foreach inside async series nodejs

I'm working on node async library. I'm not able to execute in the sequence i want. i don't know where I'm going wrong
here is the code.. in comments i have defined order number..
currently its executing in 2,3,4,5,1 order i want in 1,2,3,4,5 order ....kindly help
function getAsExhibitors(req, res) {
//getting all exhibitors against an event
var exhibitors = [];
var eac_app_names = [];
async.series([function(callback){
models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) {
exhibitors = myExhibitors;
callback();
});
},function(callback){
async.forEach(exhibitors,function(exhibitor,callback){
models.Eac.findById(exhibitor.eventid).exec(function(err,eac){
eac_app_names[exhibitors.indexOf(exhibitor)]=eac;
console.log("-----------------1--------------"+eac_app_names);
});
console.log("-----------------2--------------"+eac_app_names);
callback();
},function(err) {
console.log("-----------------3--------------"+eac_app_names);
callback();
});
}],function(err) { //This function gets called after the two tasks have called their "task callbacks"
if (err) return next(err);
//Here locals will be populated with 'exhibitors' and 'apps'
console.log("-------------------------4------"+eac_app_names);
console.log("-------------------------5------"+eac_app_names.name);
res.locals.exhibitors = exhibitors;
res.locals.eac_app_names = eac_app_names;
res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names});
});
};
All mongoose methods work as asynchronous.In your scenario try this way:
function getAsExhibitors(req, res) {
//getting all exhibitors against an event
var exhibitors = [];
var eac_app_names = [];
async.series([function(callback){
models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) {
exhibitors = myExhibitors;
callback();
});
},function(callback){
async.forEach(exhibitors,function(exhibitor,callback){
models.Eac.findById(exhibitor.eventid).exec(function(err,eac){
eac_app_names[exhibitors.indexOf(exhibitor)]=eac;
console.log("-----------------1--------------"+eac_app_names);
console.log("-----------------2--------------"+eac_app_names);
callback();
});
},function(err) {
console.log("-----------------3--------------"+eac_app_names);
callback();
});
}],function(err) { //This function gets called after the two tasks have called their "task callbacks"
if (err) return next(err);
//Here locals will be populated with 'exhibitors' and 'apps'
console.log("-------------------------4------"+eac_app_names);
console.log("-------------------------5------"+eac_app_names.name);
res.locals.exhibitors = exhibitors;
res.locals.eac_app_names = eac_app_names;
res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names});
});
};
You are welcome to use es6 with generator.
Try out co-foreach-series to get each of the array element and execute async function one by one.
ForEach Series Example
foreach(yourArray, function(element, index) {
// Each of this function will be executed one after one
co(function*() {
// Do some async task, and wait until this task be finished
yield yourAsyncFunc();
yield doOtherAsyncTasks();
})
})

Mongoose with async queue & waterfall

I aim to import large amount of data by Mongoose. As a newbie, I fail to setup the flow control properly with various mechanisms by async. Glad if someone could point to an appropriate solution. Thanks.
var async = require('async'),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Cat = mongoose.model('Cat', { name: String });
// Imagine this is a huge array with a million items.
var content = ['aaa', 'bbb', 'ccc'];
var queries = [];
content.forEach(function(name) {
queries.push(function(cb) {
var obj = new Cat({ name: name });
obj.save(function(err) {
console.log("SAVED: " + name);
console.log(err);
});
return true;
});
});
// FAILED: async.parallel adds all content to db,
// but it would exhaust the resource with too many parallel tasks.
async.parallel(queries, function(err, result) {
if (err)
return console.log(err);
console.log(result);
});
// FAILED: save the first item but not the rest
async.waterfall(queries, function(err, result) {
if (err)
return console.log(err);
console.log(result);
});
// FAILED: same as async.waterfall, async.queue saves the first item only
var q = async.queue(function(name, cb) {
var obj = new Cat({ name: name });
obj.save(function(err) {
console.log("SAVED: " + name);
console.log(err);
});
})
q.push(content, function (err) {
console.log('finished processing queue');
});
I think eachLimit or eachSeries fit your situation best:
var content = ['aaa', 'bbb', 'ccc'];
async.eachLimit(content, 10, function(name, done) {
var obj = new Cat({ name : name });
obj.save(done);
// if you want to print some status info, use this instead:
//
// obj.save(function(err) {
// console.log("SAVED: " + name);
// console.log(err);
// done(err);
// });
//
}, function(err) {
// handle any errors;
});
With eachLimit, you can run an X amount of queries 'in parallel' (10 in the example above) to speed things up without exhausting resources. eachSeries will wait for the previous save before it continues with the next, so effectively saving one object at a time.
Notice that with each*, you won't get a list with (saved) objects back (it's a bit of a fire-and-forget mechanism where you're not interested in the outcome, bar any errors). If you do want a list of saved objects in the end, you can use the equivalent map* functions: mapLimit and mapSeries.

Resources