Mongoose returned array after saving
I am trying to return an updated object as JSON where the update was supposed to set an array of object ids. I want the returned object to object to this array being full. For example, I have the following (simplified) model:
var UserSchema = new mongoose.Schema({
username: {type: String, unique: true, required: true},
friends: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
});
In my controller, I have:
exports.saveFriends = function(req, res) {
User.findById(req.params.user_id, function(err, user) {
// req.body.friends is JSON list of objectIDs for other users
user.friends = req.body.friends
user.save(function(err) {
user.populate({path: 'friends'}, function(err, ticket) {
if (err) {
res.send(err);
} else {
res.json(user);
}
});
});
});
}
This actually stores the array properly as the ObjectID, but the user of the response always shows "[]" as an array of friends.
Does anyone see my problem?
+3
source to share
No one has answered this question yet
Check out similar questions: