1
votes

I have model,virtual in it ref to User model. How to get user name without calling User model also tour.who print multiple users from diff tour.

//console.log(user);
print multiple user instead of specific to trip.

    var TourSchema = new Schema({
        user:{ type: Schema.ObjectId, ref: 'User' },
        tour:String,
        tour_slug:String,
        date:{type:Date, default: Date.now},
        tour_date:Date,
    });

    TourSchema
        .virtual("who")
        .get(function(){
            User.findOne({'fb._id':this.user,function(err,user){
                if(err)console.log(err);
                else
                    //console.log(user);
                    return user.full;
            });

        });

1
Try the populate funcitonAviv Lo

1 Answers

0
votes

If you don't want to start a new query, you could .populate the user if you query the tour object.

Tour.findOne().populate("user", "name").exec(function(err,user){
    if(err) console.log(err);
    else
        //console.log(user);
        //user.full;
});