node.js - mongoose obtains user information through UserId and cannot save it to the object
伊谢尔伦
伊谢尔伦 2017-06-05 11:08:53
0
2
1113
const ep = new eventproxy();
  ep.fail(next);

  EveryDay.getRankByTodayDate(TodayDate, ep.done('ranks'));

  ep.all('ranks', function(ranks){
    ranks.forEach(function(rank){
      User.getUserById(rank.userId, ep.done(function(user){
        rank.nickName = user.nickName; //这里不生效
        console.log(rank,rank.nickName); rank.nickName 有值,但是打印出的rank没有nickName 属性
        ep.emit('user');
      }))
    });
rank.nickName = user.nickName;

Why does assigning a value directly like this not take effect? How should we save the queried user nickname into the data object?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
某草草

The reason is @nightclub小新新’s answer.

Currently, I use this method to deal with:

 EveryDay.find({TodayDate}).lean()
    .exec((err, ranks) => {
    ...
    }
    )

I will add more when I find better answers later.

左手右手慢动作

You will know by debugging the data structure of the query results of mongoose. Only the data defined by your schema can be operated on mongoose data. Only the get/set methods are available. Other undefined attributes cannot be operated unless they are saved to _doc. in or converted into a normal object

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template