I have 2 collections (User and Offer) and I use async to do a series of operations, but the last document is not removed.
async.series([
function (callback) {
User.findOne({_id: req.user._id}, function (err, result) {
if (err) {
return callback(err);
}
user = result;
callback();
});
},
function (callback) {
Offer.find({_owner: user._id}, function (err, offers) {
if (err) {
return callback(err);
}
var i = offers.length;
while (i--) {
var offer = offers[i];
Offer.remove(offer, function (err) {
return callback(err);
});
}
callback();
});
},
function (callback) {
User.remove(user, function (err) {
if (err) {
return callback(err);
}
});
callback();
}
], function (err) {
if (err) {
return res.status(503).json({message: err});
}
res.status(200).json({message: 'User and offers successfully deleted'});
});
I turned mongoose debugging on and I see that 8 documents for this user are removed (as expected). Example message:
Mongoose: users.remove({ active: true, role: 'user', favoredOffers: [],
salt: 'qIADYcMS3SiAuF3M007E9g==', hashedPassword: '**', emailVerified: true,
name: 'Test', email: 'test1#test.com', emailVerificationToken: '***',
updatedAt: new Date("Mon, 21 Sep 2015 00:52:58 GMT"),
createdAt: new Date("Mon, 21 Sep 2015 00:52:58 GMT"),
_id: ObjectId("55ff54ea1d6201ff42ea0045") }) {
safe: { w: 'majority', wtimeout: 10000 } }
When I run a test the result is, that this one document remains. At first I thought my test runs too fast (async.waterfall) but then I switched to async.series and the behaviour remains and when I look into the test-db the document is still there.
Test code:
it.only('should allow deleting my account and all my offers if my password was right', function (done) {
async.series([function (callback) {
login(server, server.delete('*url*'), 'test#test.com', 'testpwd1', function (server) {
server
.send({password: 'testpwd1'})
.expect(200)
.end(callback);
});
}, function (callback) {
User.findOne({email: 'test1#test.com'}, function (err, user) {
callback(user ? new Error('user was still found in mongo') : null); //misuse not found user as 'error'-object
});
}, function (callback) {
Offer.find({_owner: user1._id}, function (err, offers) {
expect(offers).to.have.length(0);
callback();
});
}], done);
});
Result: Uncaught AssertionError: expected [ Array(1) ] to have a length of 0 but got 1
Wether I am missing something or I don't understand the behaviour of the mongdb.
Whilst the operations are being called in series per those that are split in the async.series the problem here is the while loops are not asynchronously controlled, and therefore the callbacks to move to the next stage are being called before all removal is complete.
You could use async.whilst to control the callbacks from the .remove() operations, but that would be overkill since you really should just be issuing ther "query" directly to .remove() rather than trying to work with a list returned by .find():
async.series(
[
function (callback) {
Offer.remove({ "_owner": req.user._id}, callback);
},
function (callback) {
User.remove({ "_id": req.user._id },callback);
}
],
function (err,results) {
if (err) {
res.status(503).json({message: err});
} else {
if ( Array.sum(results) == 0 ) {
res.status(404).json({message: 'User not found'});
} else {
res.status(200).json({message: 'User and offers successfully deleted'});
}
}
}
);
Of course if the result of the remove operations did not remove anything, then you know the input did not match the user.
The async library allows callback passing so you don't need to control each stage. Any errors immediately go to the end block, along with the results of each stage. Also look at parallel instead for this case as one stage is not dependent on the other to complete first.
Related
I am trying to extend the amount of fields that our API is returning. Right now the API is returning the student info by using find, as well as adding some information of the projects by getting the student info and using findOne to get the info about the project that the student is currently registered to.
I am trying to add some information about the course by using the same logic that I used to get the project information.
So I used the same findOne function that I was using for Projects and my logic is the following.
I created a variable where I can save the courseID and then I will put the contents of that variable in the temp object that sending in a json file.
If I comment out the what I added, the code works perfectly and it returns all the students that I require. However, when I make the additional findOne to get information about the course, it stops returning anything but "{}"
I am going to put a comment on the lines of code that I added, to make it easier to find.
Any sort of help will be highly appreciated!
User.find({
isEnrolled: true,
course: {
$ne: null
}
},
'email pantherID firstName lastName project course',
function(err, users) {
console.log("err, users", err, users);
if (err) {
return res.send(err);
} else if (users) {
var userPromises = [];
users.map(function(user) {
userPromises.push(new Promise(function(resolve, reject) {
///////// Added Code START///////
var courseID;
Course.findOne({
fullName: user.course
}, function(err, course) {
console.log("err, course", err, course);
if (err) {
reject('')
}
courseID = course ? course._id : null
//console.log(tempObj)
resolve(tempObj)
}),
///// ADDED CODE END //////
Project.findOne({
title: user.project
}, function(err, proj) {
console.log("err, proj", err, proj);
if (err) {
reject('')
}
//Course ID, Semester, Semester ID
//map to custom object for MJ
var tempObj = {
email: user.email,
id: user.pantherID,
firstName: user.firstName,
lastName: user.lastName,
middle: null,
valid: true,
projectTitle: user.project,
projectId: proj ? proj._id : null,
course: user.course,
courseId: courseID
}
//console.log(tempObj)
resolve(tempObj)
})
}))
})
//async wait and set
Promise.all(userPromises).then(function(results) {
res.json(results)
}).catch(function(err) {
res.send(err)
})
}
})
using promise could be bit tedious, try using async, this is how i would have done it.
// Make sure User, Course & Project models are required.
const async = require('async');
let getUsers = (cb) => {
Users.find({
isEnrolled: true,
course: {
$ne: null
}
}, 'email pantherID firstName lastName project course', (err, users) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
let findCourse = (users, cb) => {
async.each(users, (user, ecb) => {
Project.findOne({title: user.project})
.exec((err, project) => {
if (!err) {
users[users.indexOf(user)].projectId = project._id;
ecb();
} else {
ecb(err);
}
});
}, (err) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
let findProject = (users, cb) => {
async.each(users, (user, ecb) => {
Course.findOne({fullName: user.course})
.exec((err, course) => {
if (!err) {
users[users.indexOf(user)].courseId = course._id;
ecb();
} else {
ecb(err);
}
});
}, (err) => {
if (!err) {
cb(null, users);
} else {
cb(err);
}
});
};
// This part of the code belongs at the route scope
async.waterfall([
getUsers,
findCourse,
findProject
], (err, result) => {
if (!err) {
res.send(result);
} else {
return res.send(err);
}
});
Hope this gives better insight on how you could go about with multiple IO transactions on the same request.
I am using mLab for database and mongoose in node js.I am using swagger which should not cause any problem. I have following schemas. when user request, I need to return movie and review together if (review = true) in query. One movie may have multiple reviews. first, I have to find all the movies in the database. when I find movies, I have to go through each of them, look for any reviews in another database and somehow attach them in movie. I need to return all the movies in one package because it will be used in getAll fucntion. No matter what I do it only returning movies without reviews.
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
movie: {type: String, required: true},
reviewer: {type: String, required: true},
rating: {type: Number, required:true, min: 1, max: 5},
text: {type: String, required: true},
})
var movieSchema = new mongoose.Schema({
Title: {type: String, required: true, unique: true},
YearReleased: {type: Number, required: true},
Actors: [{
Name: {type: String, required: true}
}]
})
.
function getAll(req,res,next){
db.Movies.find({},function(err,movies){
if(err) throw {err:err};
if(req.swagger.params.review.value === false)
res.send({Movielist: movie});
else {
movies.forEach(function(movie, index){
db.Reviews.find({movie:movies[index].Title}, function (err, review) {
if(err) throw {err:err}
movies[index].Review = review // I am trying to populate each movie with reviews
});
});
res.send({Movielist: movies});
}
});
}
res.send is called before the db reviews result is received:
movies.forEach(function(movie, index){
// this call is asynchronous, res.send will run before the callback
db.Reviews.find({movie:movies[index].Title}, function (err, review) {
if(err) throw {err:err}
movies[index].Review = review // I am trying to populate each movie with reviews
});
});
res.send({Movielist: movies});
You can use promises to wait for the results. I'll give an example with async/await. Using promises without async/await is also an option.
async function getAll(req,res,next) {
try {
let movies = await getMovies();
res.send(movies);
} catch (err) {
console.log(err.stack);
}
}
async function getMovies () {
return new Promise(function (resolve, reject) {
db.Movies.find({},function(err,movies){
if(err) reject(err);
if(req.swagger.params.review.value === false)
resolve({Movielist: movie});
else {
movies.forEach(function(movie, index){
let review = await getReviews();
movies[index].Review = review
});
resolve({Movielist: movies});
}
});
});
}
async function getReviews (movie, index) {
return new Promise(function (resolve, reject) {
db.Reviews.find({movie:movies[index].Title}, function (err, review) {
if(err) reject({err:err});
resolve(review);
});
});
}
This could need some tweaks as I have not tested it. I hope it gives the general idea of how to fix the issue.
Without async/await you can call the promises and run ".then" to process the results. If you haven't used promises a few google searches should help understand how .then works.
First thank you #curtwphillips
however, I have found short way to do this. But for this to work, movie and review have to in same database as different collection. If you use different database, it doesn't work
function getAll(req,res,next) {
if (req.query.review === 'true') {
db.Movies.aggregate([
{
$lookup: {
from: "reviews",
localField: "Title",
foreignField: "movie",
as: "review"
}
}
], function (err, result) {
if (err) throw {err: err};
else {
res.send({Movielist: result});
}
});
}
else {
db.Movies.find({}, function (err, movies) {
if (err) throw {err: err};
res.send({Movielist: movies})
})
}
}
I have a big issue with my function in sails.js (v12). I'm trying to get all userDetail using async (v2.3) for deep populating my user info:
UserController.js:
userDetail: function (req, res) {
var currentUserID = authToken.getUserIDFromToken(req);
async.auto({
//Find the User
user: function (cb) {
User
.findOne({ id: req.params.id })
.populate('userFollowing')
.populate('userFollower')
.populate('trips', { sort: 'createdAt DESC' })
.exec(function (err, foundedUser) {
if (err) {
return res.negotiate(err);
}
if (!foundedUser) {
return res.badRequest();
}
// console.log('foundedUser :', foundedUser);
cb(null, foundedUser);
});
},
//Find me
me: function (cb) {
User
.findOne({ id: currentUserID })
.populate('myLikedTrips')
.populate('userFollowing')
.exec(function (err, user) {
var likedTripIDs = _.pluck(user.myLikedTrips, 'id');
var followingUserIDs = _.pluck(user.userFollowing, 'id');
cb(null, { likedTripIDs, followingUserIDs });
});
},
populatedTrip: ['user', function (results, cb) {
Trip.find({ id: _.pluck(results.user.trips, 'id') })
.populate('comments')
.populate('likes')
.exec(function (err, tripsResults) {
if (err) {
return res.negotiate(err);
}
if (!tripsResults) {
return res.badRequest();
}
cb(null, _.indexBy(tripsResults, 'id'));
});
}],
isLiked: ['populatedTrip', 'me', 'user', function (results, cb) {
var me = results.me;
async.map(results.user.trips, function (trip, callback) {
trip = results.populatedTrip[trip.id];
if (_.contains(me.likedTripIDs, trip.id)) {
trip.hasLiked = true;
} else {
trip.hasLiked = false;
}
callback(null, trip);
}, function (err, isLikedTrip) {
if (err) {
return res.negotiate(err);
}
cb(null, isLikedTrip);
});
}]
},
function finish(err, data) {
if (err) {
console.log('err = ', err);
return res.serverError(err);
}
var userFinal = data.user;
//userFinal.trips = data.isLiked;
userFinal.trips = "test";
return res.json(userFinal);
}
);
},
I tried almost everthing to get this fix but nothing is working...
I am able to get my array of trips(data.isLiked) but I couldn't get my userFInal trips.
I try to set string value on the userFinal.trips:
JSON response
{
"trips": [], // <-- my pb is here !!
"userFollower": [
{
"user": "5777fce1eeef472a1d69bafb",
"follower": "57e44a8997974abc646b29ca",
"id": "57efa5cf605b94666aca0f11"
}
],
"userFollowing": [
{
"user": "57e44a8997974abc646b29ca",
"follower": "5777fce1eeef472a1d69bafb",
"id": "5882099b9c0c9543706d74f6"
}
],
"email": "test2#test.com",
"userName": "dany",
"isPrivate": false,
"bio": "Hello",
"id": "5777fce1eeef472a1d69bafb"
}
Question
How should I do to get my array of trips (isLiked) paste to my user trips array?
Why my results is not what I'm expecting to have?
Thank you for your answers.
Use .toJSON() before overwriting any association in model.
Otherwise default toJSON implementation overrides any changes made to model associated data.
var userFinal = data.user.toJSON(); // Use of toJSON
userFinal.trips = data.isLiked;
return res.json(userFinal);
On another note, use JS .map or _.map in place of async.map as there is not asynchronous operation in inside function. Otherwise you may face RangeError: Maximum call stack size exceeded issue.
Also, it might be better to return any response from final callback only. (Remove res.negotiate, res.badRequest from async.auto's first argument). It allows to make response method terminal
I'm specifically using mongoose, although I don't believe that matters that much. For example, say I have a collection called MongoQueue and I add a few people to that queue.
`MongoQueue.save {function(err, firstPerson) {
if (err) console.log(err);
MongoQueue.save {function(err, secondPerson) {
if (err) console.log(err);
MongoQueue.save {function(err, thirdPerson) {
if (err) console.log(err);
}}}`
How do I retrieve the person who was first saved to MongoQueue? Or....how does the findOne() method of mongoDB narrow down it's search? Can I specify behavior of findOne() to choose the oldest document in the collection? Or must I do a sort on the collection (this would be last resort), and if so how would I sort by timestamp?
Yes you can specify the behavior of .findOne() as is best shown in the native driver documentation. The only difference there is that in the mongoose implemetation the "options" document must be the "third" argument passed to the method.
So you can supply a "sort" specification to this as is shown in the available options:
Queue.findOne({ },null,{ "sort": { "_id": -1 } },function(err,doc) {
Just for additional information you can do this in the MongoDB shell with the following, using the $orderby query option:
db.collection.findOne({ "$query": { }, "$orderby": { "_id": -1 } })
Also the .findOne() method may only return one document, but it really is just a wrapper around .find() so all of the modifiers apply. The wrapping just calls .next() on the returned cursor, returns the document and discards the cursor.
This longer example shows different ways in which this can be applied:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/sequence');
var queueSchema = new Schema({
name: String,
same: { type: String, default: "same" }
});
var Queue = mongoose.model( "Queue", queueSchema );
var count = 0;
async.series(
[
// Remove any documents
function(callback) {
Queue.remove(function(err) {
if (err) throw err;
callback();
});
},
// Insert some new ones
function(callback) {
async.eachSeries(
["one","two","three"],
function(item,callback) {
var queue = new Queue({ name: item });
queue.save(function(err,doc) {
if (err) throw err;
console.dir(doc);
callback(err,doc);
});
},
function(err) {
callback(err);
}
);
},
function(callback) {
async.whilst(
function() { return count < 2 },
function(callback) {
count++
async.series(
[
// findOne is just the first one
function(callback) {
Queue.findOne({ "same": "same" },function(err,doc) {
if (err) throw err;
console.log( "FindOne:\n%s", doc );
callback();
});
},
// Or is sorted
function(callback) {
Queue.findOne(
{ "same": "same" },
null,
{ "sort": { "_id": -1 } },
function(err,doc) {
if (err) throw err;
console.log("FindOne last:\n%s", doc );
callback();
}
);
},
// find is ordered but not singular
function(callback) {
async.eachSeries(
["first","last"],
function(label,callback) {
var direction = ( label == "first" ) ? 1 : -1;
var query = Queue.find({ "same": "same" })
.sort({ "_id": direction })
.limit(1);
query.exec(function(err,docs) {
if (err) throw err;
console.log( ".find() %s:\n%s", label, docs[0] );
callback();
});
},
function(err) {
callback();
}
);
},
// findAndModify takes a sort
function(callback) {
Queue.findOneAndUpdate(
{ "same": "same" },
{ "$set": { "same": "different" } },
{ "sort": { "_id": -1 } },
function(err,doc) {
if (err) throw err;
console.log( "findOneAndUpdate:\n%s", doc );
callback();
}
);
}
],function(err) {
callback();
}
);
},
function(err) {
callback();
}
);
}
],function(err) {
console.log("done");1
mongoose.disconnect();
}
);
The findOne() function returns documents in the natural order, which is the order on disk. You cannot count on this returning the least recently inserted document. To return the least recently inserted document, you can use the find() function with an inverse sort on _id and a limit of 1, assuming _id is a default ObjectId generated for you, or on a timestamp (_id is built, in part, from a timestamp, which is why it works with _id). If you use _id (which is always indexed) or index the timestamp field, this will be very fast.
The code below works, it updates a record or creates one if it doesn't exist yet. However, I'd like to combine this findOneAndUpdate() statement with the populate() method in order to populate the "user" of my object. What would be the right way to add the populate("user") statement to this logic?
I tried adding the populate() method after the findOneAndUpdate finishes but that returns an error saying that this method doesn't exist. I'm running the latest version of mongoose.
LoyaltyCard.findOneAndUpdate({ business: businessid}, { $set: newCard, $inc: { stamps: +1 } }, { upsert: true}, function(err, card){
if(err)
{
}
else
{
}
res.json(result);
});
Use exec() instead of a callback parameter:
LoyaltyCard.findOneAndUpdate(
{business: businessid},
{$set: newCard, $inc: {stamps: +1}},
{upsert: true}
)
.populate('user')
.exec(function(err, card) {
if (err) {
// ...
} else {
res.json(result);
}
});
With async/await I removed the exec
const getLoyaltyCard = async () => {
const results = await LoyaltyCard.findOneAndUpdate(
{ business: businessid },
{ $set: newCard, $inc: { stamps: + 1 } },
{ upsert: true }
)
.populate('user')
return results
}
You can also add a populate object in the 3rd parameter of .findOneAndUpdate() as one of the option, like this:
LoyaltyCard.findOneAndUpdate(
{ business: businessid },
{ $set: newCard, $inc: { stamps: +1 } },
{ upsert: true, populate: { path: 'user' } }
)
.exec(function(err, card) {
if (err) {
// ...
} else {
res.json(result);
}
});
Just enhancing #rahulchouhan's answer:
You can add the populate as one of the options which is the third parameter of findOneAndUpdate function and it works just like any other promise (then, catch)
LoyaltyCard.findOneAndUpdate(
{ business: businessid },
{ $set: newCard, $inc: { stamps: +1 } },
{ upsert: true, populate: { path: 'user' } }
)
.then(card => {
res.status(200).json(card);
}).catch(err => {
res.status(500).json({ message: err.message});
});