【问题标题】:What's wrong with this mongoose query?这个猫鼬查询有什么问题?
【发布时间】:2021-11-20 00:34:09
【问题描述】:
    const images = await tbl
      .find({
        creator_id: req.user._id,
      })
      .select({
        creator_id: 0,
      })
      .then((images) =>
        images.forEach((image) => {
          image.file_name = process.env.IMAGE_HOST_URL + image.file_name;
        })
      );

它在 .then 位中失败。不知道为什么。

【问题讨论】:

  • 您遇到了什么错误?你可以分享一些日志
  • 其实不是抛出错误,而是返回一个空数组。这不是预期的。没有.then 位,列表不为空。
  • 您正在使用awaitthen。使用一个或另一个。
  • 啊,我想我知道为什么了。删除第一行的等待。或突破 .then 以自行发生。我会发布一个答案。

标签: javascript node.js mongodb express mongoose


【解决方案1】:

问题发生是因为您的承诺在您调用 .then 之前已经解决。删除 await 或打破 .then 以保持自己的状态

   const images = tbl
  .find({
    creator_id: req.user._id,
  })
  .select({
    creator_id: 0,
  })
  .then((images) =>
    images.forEach((image) => {
      image.file_name = process.env.IMAGE_HOST_URL + image.file_name;
    })
  );

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2011-09-29
    • 2011-10-13
    • 1970-01-01
    • 2010-09-07
    • 2010-10-04
    • 2011-01-15
    相关资源
    最近更新 更多