Mongoose update after save - javascript

I'm missing something about callbacks with Mongoose save function.
I am trying to insert a new transaction, then if that is successful, update a user account. The problem, I believe, This cause all people to be updated with the last person's amount. What am I trying to do is update a document after saving another document.
Here is the code. Please let me know what I am doing wrong.
Thanks in advance.
//find all schedules
SchedModel.find({ Day_Of_Week: day }, null, null, function (err, Sched) {
if (!err) {
//iterate through what we find
for (var key in Sched) {
if (Sched.hasOwnProperty(key)) {
var val = Sched[key];
console.log("val : " + val);
var Sched_Descr = day || ' Sched Trans';
var this_trans = new TransModel({
mID: val.K_Id,
mDate: today,
mDescr: Sched_Descr,
mAmt: val.mAmt
});
//insert the new trans
this_trans.save(function (err, trans) {
if (!err) {
//when we insert new trans, get the update model
MyModel.findById(val.K_Id, function (err, Model) {
Model.Balance = Model.Balance + val.mAmt;
//save model, this update to the last in the list
Model.save(function (err) {
if (!err) {
console.log("updated");
} else {
console.log(err);
}
});
});
} else {
return console.log(err);
}
});
}
}
} else {
console.log(err);
};
});

Update: ES6's let solves this pretty trivially, just replace var with let in your original code and it should work.
Your this_trans and such variables aren't unique in each iteration of the for-in loop. You might wanna wrap it in a self-executing anonymous function scope ((function(){})())
//find all schedules
SchedModel.find({ Day_Of_Week: day }, null, null, function (err, Sched) {
if (!err) {
//iterate through what we find
for (var key in Sched) {
(function(key){ // self-executing anonymous function scope
if (Sched.hasOwnProperty(key)) {
var val = Sched[key];
console.log("val : " + val);
var Sched_Descr = day || ' Sched Trans';
var this_trans = new TransModel({
mID: val.K_Id,
mDate: today,
mDescr: Sched_Descr,
mAmt: val.mAmt
});
//insert the new trans
this_trans.save(function (err, trans) {
if (!err) {
//when we insert new trans, get the update model
MyModel.findById(val.K_Id, function (err, Model) {
Model.Balance = Model.Balance + val.mAmt;
//save model, this update to the last in the list
Model.save(function (err) {
if (!err) {
console.log("updated");
} else {
console.log(err);
}
});
});
} else {
return console.log(err);
}
});
}
})(key);
}
} else {
console.log(err);
};
});

Related

Variable inaccessible inside callback function

I have a variable deleteFlag which is inaccessible inside a function even though the variable's scope is global.
Explanation (Pls refer my code simultaneously):
Here, I am trying to get a MongoDB collection details, the collection store a date document (result[i].date). The variable difResult stores the difference between the current date and the date fetched from MongoDB. And let's say if the value of difResult is more than a specific threshold then handle respective if-else conditions.
My if block i.e. if(difResult>20000) has a child-process, exec function and a callback function to delete MongoDB collection, now in this function I am trying to access var deleteFlag which is sort inaccessible.
Why? And how can I make is accessible inside my function?
app.js
MongoClient.connect("mongodb://localhost:27017/", {
useUnifiedTopology: true
}, function(err, db) {
if (err) throw err;
var dbo = db.db("dbName");
dbo.collection("colName").find({}).toArray(function(err, result) {
if (err) throw err;
for (var i = 0; i < result.length; i++) {
var difResult = Math.round((today - result[i].date));
var deleteFlag = result[i].date; // Declared here and should be accessbile within the function
console.log("Delete Flag " + deleteFlag.toISOString()); //Show correct value here
console.log("Result Date " + result[i].date);
if (difResult > 20000) {
var result2 = cp.exec("rm -rf /path/" + deleteFlag.toISOString(), function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return res1.status(500).json({
error: "Failed!"
});
} else {
MongoClient.connect("mongodb://localhost:27017/", {
useUnifiedTopology: true
}, function(err, db) {
console.log("Delete Flag From Collection ", +deleteFlag.toISOString());
//The above console log gives NaN or null value
//Suggest that var deleteFlag is not accessible inside this callback function
if (err) throw err;
var dbo = db.db("dbName");
var myquery = {
date: deleteFlag
};
dbo.collection("colName").deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log("1 document deleted");
db.close();
});
});
}
});
} else {
console.log("Else msg");
}
}
db.close();
});
});
You don't have to call the database twice you can optimize your code and use it like this
MongoClient.connect("mongodb://localhost:27017/", {
useUnifiedTopology: true
}, function(err, db) {
if (err) throw err;
var dbo = db.db("dbName");
dbo.collection("colName").find({}).toArray(function(err, result) {
if (err) throw err;
for (var i = 0; i < result.length; i++) {
var difResult = Math.round((today - result[i].date));
var deleteFlag = result[i].date; // Declared here and should be accessbile within the function
console.log("Delete Flag " + deleteFlag.toISOString()); //Show correct value here
console.log("Result Date " + result[i].date);
if (difResult > 20000) {
var result2 = cp.exec("rm -rf /path/" + deleteFlag.toISOString(), function(error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
return res1.status(500).json({
error: "Failed!"
});
} else {
var myquery = {
date: deleteFlag
};
dbo.collection("colName").deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log("1 document deleted");
});
}
});
} else {
console.log("Else msg");
}
}
db.close();
});
});
However if for whatever reason you need to call the database twice then store deleteFlag values in an array and then access the array wherever you like

Run loop on the server with node.js

Let's say i need to constantly collecting some data from a lot of clients and in parallel running some complex loop that solving some stuff with this data. How can i do it? Should i just write this in my piece of code:
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/views/index0.html');
});
io.sockets.on('connection', function(socket) {
// SOME STUFF WITH THE SOCKET
socket.on('disconnect', function(data) {
//SOME OTHER STUFF
});
});
while(...) {
//THE LOOP STUFF
}
Or i need to use the setTimeout() and setInterval() functions? How can i do the loop on the server that runs in parallel with the callbacks' stuff?
Don’t use while for make it, this block a thread. setTimeout() will run only once. You need to use setInterval() function.
You can use the async module to handle the async operation with the callback or use promise to avoid callback.
Here how I handle a complex async for each operation, that might helpfull to you get idea handeling ayncs forach
var cond = { _schedule: schedule_id }; // find curse by schedule id
Course.find(cond, function (err, courses) {
if (err) {
callback({ "success": false, "message": "Not able to update" });
} else {
async.forEachLimit(courses, 1, function (course, coursesCallback) {
async.waterfall([
function (callback) {
var schedule_date = moment(change_data.date).format('YYYY-MM-DD') + "T00:00:00.000Z"
var Assignmentcond = {
assignment_schedule_order: {
$gte: schedule_date
},
_course: course._id,
_schedule: schedule_id,
_user: userid
};
Assignment.find(Assignmentcond)
.populate({
path: '_course',
})
.lean()
.sort({ assignment_schedule_order: 1 })
.exec(function (err, AssignmentList) {
if (err) {
callback(null, '');
} else {
//console.log("------------------AssignmentList---------------------------");
//console.log(AssignmentList);
async.forEachLimit(AssignmentList, 1, function (ThisAssignmentCell, ThisAssignmentCellCallback) {
async.waterfall([
function (callback) {
var SearchObj = items;
var lebelObject = {};
for (var i = 0, flag = 0, insert = 0; i < SearchObj.length; i++) {
if (SearchObj[i].date == ThisAssignmentCell.assignment_schedule_date) {
flag = 1;
}
if (flag == 1 && SearchObj[i].label != "") {
if (ThisAssignmentCell.day == SearchObj[i].day_index) {
insert = 1;
var lebelObject = SearchObj[i];
break;
}
}
}
callback(null, ThisAssignmentCell, lebelObject, insert);
},
function (ThisAssignmentCell, SearchObj, insert, callback) {
console.log('----------------------');
console.log('ThisAssignmentCell', ThisAssignmentCell);
console.log('SearchObj', SearchObj);
console.log('----------------------');
if (insert > 0) {
var query = { _id: ThisAssignmentCell._id },
fields = {
assignment_date: moment(SearchObj.date).format('MM/DD/YYYY'),
assignment_schedule_date: moment(SearchObj.date).format('YYYY-MM-DD'),
assignment_schedule_order: new Date(SearchObj.date),
day: SearchObj.day_index,
dayNum: SearchObj.weekday_num
},
options = { upsert: false };
Assignment.update(query, fields, options, function (err, affected) {
callback(null, '');
});
} else {
// var cond = { _id: ThisAssignmentCell._id};
// Assignment.remove(cond)
// .exec(function (err, cnt) {
// callback(null, '');
// });
}
}
], function (err, result) {
// result now equals 'done'
console.log('done')
ThisAssignmentCellCallback();
});
}, function (err) {
console.log("Assignment For Loop Completed");
callback(null, AssignmentList);
});
}
});
}
], function (err, result) {
// result now equals 'done'
console.log('done')
coursesCallback();
});
}, function (err) {
console.log("courses For Loop Completed");
});
}
});

Problems with multiple Node.js callbacks

I'm trying to solve a problem i've been having with a nodejs cronjob of mine. So basically, This request grabs my tracks from soundcloud, I loop through the results and put the data in a mongodb collection. This all works great, but now i'm adding another section to the site, so I need to grab some additional info from another collection.
I have a tracks collection, and an inventory collection. The track id is in both collections to relate the additional track data to the newly pulled tracks. So my question is how can I get this additional track data in? Below i have tried to loop through it and inject the data using mongoose's query.find() but the loops do not work together. The callback of the inventory query will run all in one for loop it seems... I'm not exactly sure whats going on there.
I'm pretty sure you can also inject a document from another collection by referencing it in the schema... but i'm unsure of how to get this working. This would obviously be a better solution as it won't require more code like this.
if anybody has any suggestions for me that would be great!
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var o = 1;
for(i=0; i < body.tracks.length; i++){
var last = (i + 1);
var track = body.tracks[i];
if( track.sharing == 'public'){
var invData;
var obj;
db.model('inventory').find({id:track.id}).exec(function(err,item){
//console.log(item[0]);
invData = item[0];
});
console.log(invData, obj);
obj = new TracksModel({
id: track.id,
sharing:track.sharing,
uri:track.uri,
description:track.description,
created_at:track.created_at,
duration:track.duration,
title:track.title,
description:track.description,
order: o,
inventory_data: invData
});
o++;
obj.save(function (err) {
if (!err) {
console.log('Track inserted successfully');
} else {
throw err;
}
});
if(last == body.length){
setTimeout(function(){
console.log("Automatically closing database connection after 5 seconds");
db.close();
}, 5000);
}
}
}
} else {
console.log('An error has occurred: ', error);
}
});
The way you are treating the query callback is wrong. You are assuming that the code starting from "console.log(invData, obj);" will be executed immediately after the db.model.find. That not the correct notion of Callback. How must put that code inside the exec callback function. You may have to use a closure.
Something like:
if (!error && response.statusCode === 200) {
var o = 1;
for(i=0; i < body.tracks.length; i++){
var last = (i + 1);
var track = body.tracks[i];
if( track.sharing == 'public'){
(function(track,last,o){
var invData;
var obj;
db.model('inventory').find({id:track.id}).exec(function(err,item){
//console.log(item[0]);
invData = item[0];
console.log(invData, obj);
obj = new TracksModel({
id: track.id,
sharing:track.sharing,
uri:track.uri,
description:track.description,
created_at:track.created_at,
duration:track.duration,
title:track.title,
description:track.description,
order: o,
inventory_data: invData
});
obj.save(function (err) {
if (!err) {
console.log('Track inserted successfully');
} else {
throw err;
}
});
if(last == body.length){
setTimeout(function(){
console.log("Automatically closing database connection after 5 seconds");
db.close();
}, 5000);
}
});
}(track,last,o);
o++;
}
}
}
Try this out:
var utils = require('restberry-utils');
var Inventory = mongoose.model('Inventory');
var Track = mongoose.model('Track');
request({
url: url,
json: true
}, function (error, response, body) {
if (error || response.statusCode !== 200) {
console.log('An error has occurred: ', error);
return;
}
utils.forEachAndDone(body.track, function(track, iter) {
if (track.sharing !== 'public') {
iter();
return;
}
Inventory.findOne({ id: track.id }, function(err, item) {
new Track({
id: track.id,
sharing: track.sharing,
uri: track.uri,
description: track.description,
created_at: track.created_at,
duration: track.duration,
title: track.title,
description: track.description,
order: o,
inventory_data: item,
}).save(function(err) {
if (err) {
throw err;
} else {
console.log('Track inserted successfully');
iter();
}
})
});
}, function() {
console.log('Done!');
setTimeout(function() {
console.log("Automatically closing database connection after 5 seconds");
db.close();
}, 5000);
})
});

nodejs return from callbacks

guys. I cant resolve some prolem with js callback return.
So, we have next func:
//Функция получения контакт листа пользователя
function get_contact_list(data) {
//Берем ID пользователя, который к нам пытается обратиться исходя из SESSION_ID
conn.query('SELECT id FROM users WHERE session_id="' + data['auth'] + '" LIMIT 1;', function(err, res) {
if (err) {
console.log(err);
}
//Разбираем результат
res.fetchAll(function(err, row) {
if (err) {
console.log(err);
}
//А теперь, собсна, выбираем контакты
conn.query('SELECT u.id, u.sname, u.fname, u.nick FROM users as u LEFT JOIN contacts AS c ON c.dep_id = u.id WHERE owner_id =' + row[0].id + ';', function(err, res) {
if (err) {
console.log(err);
}
//Разбираем результат
res.fetchAll(function(err, row) {
if (err) {
console.log(err);
}
//TAKE row HEREOF
NEED_OUT = row;
});
});
});
});
return NEED_OUT;
}
I need return variable row from 2-lvl callback, but if i create global var or write "row" in global object, it's not working. Help me pls! Thank you!
I believe you didn't get the concept of async code right. Because you example is way to long, here some pseudo code that shows you in which order async code will work.
// Timestamp: 0
function foo(input, cb) {
// Timestamp: 2
doSomeThingAsync(input, (result) {
// Timestamp: 5
result = transformResultSync(result);
cb(result);
});
// Timestamp: 3
}
// Timestamp: 1
foo('bar', function(result) {
// Timestamp: 6
console.log(result);
});
// Timestamp: 4
I hope it helps a little bit.

How can I use node async to fetch my mongoose calls?

I'm building a site with node/express/mongoose and it needs to do the following things when viewing a submission.
The problem I'm running into is doing db fetches in a non-serial fashion. For example, I'll do a few calls to fetch some data, but some of the calls might not finish until the execution context goes to the other. Tried to use the npm module, async, but am having trouble trying to figure out how I would integrate it.
Here is my code:
var getViewCount = function(submissionId) {
Submission.getSubmissionViewCount({
submissionId : submissionId
}, function(err, count) {
if (err) {
throw err;
}
if (count) {
return count;
}
});
};
var getVotes = function(submissionId) {
console.log('getvotes');
Submission.getSubmissionVotes({
submissionId : submissionId
}, function(err, votes) {
return votes;
});
};
var getSubmission = function(id) {
Submission.getSubmission({
id : id
}, function(err, submission) {
if (err) {
throw err;
}
if (submission) {
return submission;
}
});
};
var renderSubmission = function(title, submission, views) {
res.render('submission', {
title: submission.title + ' -',
submission: submission,
views: views.length
});
};
How do I use this with async? Or should I be using async.series isntead of async.async?
async.series([
function(callback) {
var submission = getSubmission(id);
callback(null, submission);
},
function(callback) {
// getViewCount(submissionId);
},
function(callback) {
// getVotes(submissionId);
},
function(callback) {
//renderSubmission(title, submission, views);
}
], function(err, results) {
console.log(results);
});
Basically I want to fetch the views and votes first and then render my submission.
TheBrain's description of the overall structural changes that you should make to your code is accurate. The basic methodology in Node is to nest a series of callbacks; very rarely should you require functions that actually return a value. Instead, you define a function that takes a callback as parameter and pass the result into that callback. Please review the code below for clarification (where cb is a callback function):
var getViewCount = function(submissionId, cb) {
Submission.getSubmissionViewCount({
submissionId : submissionId
}, function(err, count) {
if (err) {
throw err;
}
if (cb) {
cb(count);
}
});
};
var getVotes = function(submissionId, cb) {
console.log('getvotes');
Submission.getSubmissionVotes({
submissionId : submissionId
}, function(err, votes) {
if (cb) {
cb(votes);
}
});
};
var getSubmission = function(id, cb) {
Submission.getSubmission({
id : id
}, function(err, submission) {
if (err) {
throw err;
}
if (cb) {
cb(submission);
}
});
};
var renderSubmission = function(submissionId) {
getSubmission(submissionId, function (submission) {
if (!submission) {
// unable to find submission
// add proper error handling here
} else {
getViewCount(submissionId, function (viewCount) {
res.render('submission', {
title: submission.title + ' -',
submission: submission,
views: viewCount
});
});
}
};
};

Categories