我在数据库中具有reservation
模型。这就是我获取一个条目(使用猫鼬(的方式,
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
如何获得用户的价值?
如果要通过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);
}
});
我在我的架构而不是'用户'中写了'user_id',这是错误。