Node.js 使用带节点的asyn函数填充不起作用

Node.js 使用带节点的asyn函数填充不起作用,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我在我的项目中使用nodejs和mongoose,我试图使用mongoose populate填充一个对象数组(在我的例子中是trc),但我在trc属性中得到一个空数组,我不知道为什么: 功能: const getUserVehicles = async(id) =>{ return new Promise((resolve, reject) => { User.findById(id) .populate('truc') .exe

我在我的项目中使用nodejs和mongoose,我试图使用mongoose populate填充一个对象数组(在我的例子中是trc),但我在trc属性中得到一个空数组,我不知道为什么:

功能:

    const getUserVehicles = async(id) =>{
    return new Promise((resolve, reject) => {
      User.findById(id)
      .populate('truc')
      .exec(function (err, item) {
        if (err) return handleError(err);
        console.log('The item is %s', item);
        // prints "The author is Ian Fleming"
        resolve(item)
      });
    })
  }
   "verified": false,
   "userType": "Individu",
   "status": "Disponible",
   "truc": [], --here's the empty array
   "_id": "5d4435f57cd101243d1b48a6",
   "email": "test125@gmail.com",

   "createdAt": "2019-08-02T13:09:09.148Z",
   "updatedAt": "2019-08-02T13:09:09.148Z"
.....
  truc: [{ type: Schema.ObjectId, ref: 'Vehicle' }]
.....
响应:

    const getUserVehicles = async(id) =>{
    return new Promise((resolve, reject) => {
      User.findById(id)
      .populate('truc')
      .exec(function (err, item) {
        if (err) return handleError(err);
        console.log('The item is %s', item);
        // prints "The author is Ian Fleming"
        resolve(item)
      });
    })
  }
   "verified": false,
   "userType": "Individu",
   "status": "Disponible",
   "truc": [], --here's the empty array
   "_id": "5d4435f57cd101243d1b48a6",
   "email": "test125@gmail.com",

   "createdAt": "2019-08-02T13:09:09.148Z",
   "updatedAt": "2019-08-02T13:09:09.148Z"
.....
  truc: [{ type: Schema.ObjectId, ref: 'Vehicle' }]
.....
用户型号:

    const getUserVehicles = async(id) =>{
    return new Promise((resolve, reject) => {
      User.findById(id)
      .populate('truc')
      .exec(function (err, item) {
        if (err) return handleError(err);
        console.log('The item is %s', item);
        // prints "The author is Ian Fleming"
        resolve(item)
      });
    })
  }
   "verified": false,
   "userType": "Individu",
   "status": "Disponible",
   "truc": [], --here's the empty array
   "_id": "5d4435f57cd101243d1b48a6",
   "email": "test125@gmail.com",

   "createdAt": "2019-08-02T13:09:09.148Z",
   "updatedAt": "2019-08-02T13:09:09.148Z"
.....
  truc: [{ type: Schema.ObjectId, ref: 'Vehicle' }]
.....

有人能帮忙吗?

truc
类型从
Schema.ObjectId
更改为
Schema.Types.ObjectId
和mongoose v>=5支持承诺,因此您可以这样做

const getUserVehicles = id => User.findById(id).populate('truc');
然后叫它

getUserVehicles(/* id */)
  .then(console.log)
  .catch(console.log);

trc
类型从
Schema.ObjectId
更改为
Schema.Types.ObjectId
并且mongoose v>=5支持承诺,因此您可以这样做

const getUserVehicles = id => User.findById(id).populate('truc');
然后叫它

getUserVehicles(/* id */)
  .then(console.log)
  .catch(console.log);

我确认mongoose高于@5.6.7(在异步和回调模式方法中)也存在类似的问题。还在四处寻找解决办法


有人提出了一些建议,建议不要使用大量的objectId引用。

我确认mongoose高于@5.6.7的版本也存在类似的问题(无论是异步还是回调模式)。还在四处寻找解决办法


有人建议不要使用大量的objectId引用。

不相关,但将原始
console.log
作为回调传递将不起作用,因为它是在错误的上下文中执行的,您需要传递
console.log.bind(console)
:thnaks作为您的答案,但我遇到了同样的问题,truc数组为空,我想使用asyn/WAITfunctions@titilo您确定
trc
中有ID吗?在不填充的情况下过帐返回的文档。如果您只是返回一个承诺,并且
findById
已经返回了一个承诺,那么没有理由使用
异步函数
是的,tric对象中有ID,但是我仍然得到一个空的arrayUnrelated,但是将raw
console.log
作为回调传递将不起作用,因为它是在错误的上下文中执行的,您将需要传递
console.log.bind(console)
:thnaks用于您的答案,但我遇到了相同的问题,truc数组为空。我想使用asyn/awaitfunctions@titilo您确定
trc
中有ID吗?在不填充的情况下过帐返回的文档。如果您只是返回一个承诺,并且
findById
已经返回了一个承诺,那么没有理由使用
异步函数
是的,tric对象中有ID,但我仍然得到一个空数组