What's wrong with this mongoose query?

    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;
        })
      );

It fails in the .then bit. Not sure why.

Upvotes: 0

Views: 51

Answers (1)

VIC3KING
VIC3KING

Reputation: 882

The issue happens because your promise is already resolved before you call .then. remove the await or break out the .then to stay on its own

   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;
    })
  );

Upvotes: 1

Related Questions