search

Home  >  Q&A  >  body text

node.js - mongoose obtains user information through UserId and cannot save it to the object

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?

伊谢尔伦伊谢尔伦2720 days ago1092

reply all(2)I'll reply

  • 某草草

    某草草2017-06-05 11:10:53

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-06-05 11:10:53

    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

    reply
    0
  • Cancelreply