如何等待猫鼬查询被填充?

时间:2020-07-18 08:42:38

标签: node.js mongodb mongoose async-await mongoose-populate

我有一个awaited猫鼬查询,该查询也填充了一些模型。 但是子项似乎直到稍后才被填充。

是否有一种方法/需要再次等待子文档? 还是一种flush查询的方法? 我尝试了async/awaitthen的各种版本,但均未成功。

数据确实显示在客户端中,因此最终查询可以在某个地方解析。 但是当我需要处理它时,它还没有准备好。

let list = await _mongo.find({ type: 'vlog', shelves: true })
      .populate('user')
      .populate({
        path: 'product',
        model: 'product'
      })
      .sort({ createTime: -1 }).lean().exec()

console.log('list ready', list)

但是产品和用户的测试结果未定义。 更奇怪的是,以某种方式记录它们似乎有时会改变它们

当我逐步接触调试器时,我只会看到某种缓冲对象 debugger

Logger.log('no product', doc.product)

/// logs out the product!
  {
    _id: ObjectID {
      _bsontype: 'ObjectID',
      id: <Buffer 5d c9 8d b7 9b d5 eb 20 8b 44 fa 7e>
    },
    store: ObjectID {
      _bsontype: 'ObjectID',
      id: <Buffer 5d e1 e2 bf 9b d5 eb 20 8b 44 fa 86>
    },
    name: 'Benefit Precisely My Brow Pencil No. 3',
    desc: '',
    content: 'This ultra-fine eyebrow pencil comes in eight easy-to-choose shades and draws incredibly natural-looking, hair-like strokes that last 12 budge-proof hours. A few strokes of the rich yet blendable colour will transform brows from shapeless and undefined to filled and defined eyebrows.',

list ready [
  {
    _id: ObjectID {
      _bsontype: 'ObjectID',
      id: <Buffer 5e fc a7 5d 09 b4 33 5b fd c0 35 8b>
    },
    user: {
      _id: [ObjectID],
      name: 'estpehaniatjuaca',
....
    hashtag: [ 'makeup ', 'tutorial', 'makeuphangout', 'makeupnatural' ],
    content: 'Makeup Hangout Natural',
    product: [ [Object] ]
  }
]


参考:

缓冲 https://mongoosejs.com/docs/connections.html#buffering https://mongoosejs.com/docs/populate.html

0 个答案:

没有答案