我知道它的语法以及它是如何工作的,但是我无法理解内部的工作原理,为什么一个方法链同时需要另一个方法,而不是其他的时间呢?
这段代码工作正常
const cart = await Carts.findById(cartId).populate('product');但这段代码没有
let cart = await Carts.findById(cartId);
cart = await cart.populate('product');为了使它工作,我们使用了execPopulate方法,它的工作方式如下。
let cart = await Carts.findById(cartId);
cart = await cart.populate('product').execPopulate();现在,就我所读到的javascript中的方法链接而言,没有execPopulate方法,代码应该运行得很好。但我似乎不明白为什么填充不工作在现有的猫鼬对象。
发布于 2021-04-19 09:06:05
Carts.findById(cartId);返回查询对象。
当您使用await Carts.findById(cartId);时,它返回文档,因为它将解析承诺并获取结果。
等待操作员用来等待承诺。
let cart = await Carts.findById(cartId); // cart document fetched by query
cart = await cart.populate('product'); // you can't run populate method on document有效案例
const cartQuery = Carts.findById(cartId);
const cart = await cartQuery.populate('product');.execPopulate是文档上的方法,而.populate则是查询对象。
https://stackoverflow.com/questions/67157818
复制相似问题