Nodejs json对象一个属性值不打印而其他属性值打印

时间:2018-02-17 20:50:27

标签: node.js mongodb mongoose

我的数据库中有reservation模型。这是我获取一个条目(使用mongoose),

的方法
reservationModel.get_reservation_by_user_id(payload.user.id, function (err, reservations) {
      if (err) {
        console.log(err);
      } else {
        console.log(reservations);
        console.log(reservations[0].user);  // prints undefined                         
        console.log(reservations[0].from); // prints 0900
      }
    });

这是控制台输出。

Dialog Opened sucessful
[ { _id: 5a887a20734d1d041bb6a1f3,
    tutor: '5a760a1f734d1d3bd58c8d52',
    user: 'U8XDVJD26',
    date: 2018-02-17T13:53:45.415Z,
    day: 'Saturday',
    from: '0900',
    to: '1030',
    active: 'no' },
  { _id: 5a887bd2734d1d041bb6a24f,
    tutor: '5a760a1f734d1d3bd58c8e12',
    user: 'U8XDVJD26',
    date: 2018-02-17T14:00:45.415Z,
    day: 'Saturday',
    from: '0930',
    to: '1130',
    active: 'no' } ]
undefined
0900

如何获取用户的价值?

2 个答案:

答案 0 :(得分:0)

如果您想按ID获取记录,则需要使用方法findById。示例:

reservationModel.findById(payload.user.id, function (err, reservations) {
      if (err) {
        console.log(err);
      } else {
        console.log(reservations);
        console.log(reservations.user);                     
        console.log(reservations.from);
      }
    });

答案 1 :(得分:0)

我的架构中写了'user_id'而不是'用户',这就是错误。

相关问题