This is the code:
usermodel.findOne({ user: req.session.user }, function (err, user){
usermodel.find({ _id: {$in: user.follow } }, { user: true }, function (err, users){
usermodel.find({ author: { $in: users.user }}, function (err, images){
console.log(users);
console.log(images);
});
});
And this is the Schema:
var userschema = new mongoose.Schema({
user: String,
follow: [String],
message: [{
title: String,
author: String,
message: String
}],
});
The followarray contains the _ids of the users that the actual user is following. With the first usermodel.find y get the follow array. With the second usermodel.find, I get the users names:
[ { _id: 50fd9c7b8e6a9d087d000006, user: 'Mrmangado' },
{ _id: 50fd9d3ce20da1dd7d000006, user: 'kirbo' } ]
And, with the third and last usermodel.find, I'm trying to get the message of the users. I get the users' names in the previous usermodel.find, and the author of the message has the same value of the user that has created it. The problem is the way I get the users' names, I think I have to have the users' names like this way:
[{ user: 'Mrmangado' },
{ user: 'kirbo' }]
If I receive an array like this, the $in statement will work perfectly. Is there any way to get an output like this...?
Thank's advance!
Are you sure that's what you want? The author field is a user's name, not its _id? If so, you could just map the resulting object to get the name.
var usernames = users.map(function(u) { return u.user; });
//= ["Mrmangado", "kirbo"]