I have method getFollowing to get list of following user
UserSchema.statics.getFollowing = function(data, callback) {
console.log("data: ", data);
this.findOne({
name : data.name
}, function(err, docs) {
if (err)
callback("error " + err)
else if (docs) {
console.log("XXXX", docs.following_count)
mongoose.model('User').findOne({
_id : docs._id
}).populate('following', 'name screen_name avatar _id').exec(function(err, res) {
if (err)
callback(err);
// console.log('The following is %s', res)
});
callback(docs.following);
}
else callback('{"error": "user-not-found"}');
});
}
and I want to callback send to client docs.following list only I dont know when I console.log(docs.following) it print but when I callback(docs.following) it return err.
I define UserSchema like that
var UserSchema = new Schema({
created_at : { type: Date, default: Date.now }
, name : String
, hashedPass : String
, salt: String
, email : Email
, avatar : {type: String, default: "images/default_profile.png" }
, statuses_count : Number
, screen_name : String
, location : String
, about : String
, followers: [{ type: ObjectId, ref: 'User' }]
, following: [{ type: ObjectId, ref: 'User' }]
, followers_count : Number
, following_count : Number
});
I think you want to move that callback(docs.following); call into the callback (where you currently have // console.log('The following is %s', res)). Also, by convention, the first parameter of a callback is an error and the second one the data, so change callback(docs.following); to callback(null, docs.following);.