0

I have written some code that successfully pulls the value from MongoDB, to check what has been sent to the API is the same price in MongoDB. In order to get the value from MondoDB, I have to use findbyID (a Mongoose function).

For some reason, despite using async and await, the validation checks are run before the value is returned from Mongoose's findById function and the value from MongoDB is provided on the last line of the console log output, despite me thinking it should be the second console logged output.

Please could someone tell me how I can ensure the checks only begin after getting the dbPrice.price value from MongoDB and assigning it to the DBPrice variable?

Here's the code and the console output:

Code

var checkValidPrice = [];
  req.body.products.map((item) => {
    const itemPrice = item.price;
    console.log(`item price: ${itemPrice}`);
    async function checking() {
      let dbPrice = await Product.findById(item.productId, "price");
      console.log(`dbPrice: ${dbPrice.price}`);
      if (itemPrice === dbPrice.price) {
        return checkValidPrice.push("true");
      } else {
        return checkValidPrice.push("false");
      }
    }
    checking();

    console.log(`check price printing: ${checkValidPrice}`);
  });

  console.log(`func? ${checkValidPrice.includes("false")}`);

actual console output

item price: 350
check price printing: 
func? false
dbPrice: 350

expected console output

item price: 350
dbPrice: 350
check price printing: true
func? false

EDIT:

I have replicated the logic explained in the commented questions, but they do not work. For example:

      var checkValidPrice = await Promise.all(
      req.body.products.map((item) => {
      const itemPrice = item.price;

      let dbPrice = Product.findById(item.productId, "price");
      console.log("dbprice ", dbPrice.price);
      if (itemPrice === dbPrice.price) {
        return true;
      } else {
        return false;
      }
    })
  );

  console.log(`check price printing: ${checkValidPrice}`);
Texas12
  • 7
  • 3
  • 3
    you aren't `await`ing your `checking()` call which won't do what you expect inside a `map()` call anyway. – pilchard Mar 11 '23 at 18:30
  • and [Use async await with Array.map](https://stackoverflow.com/questions/40140149/use-async-await-with-array-map) – pilchard Mar 11 '23 at 18:31
  • `await` doesn't really pause execution in any case. It's all about Promise behavior. – Pointy Mar 11 '23 at 19:00
  • @pilchard, thank you for the link but, because I'm a beginner, the answer doesn't make as much sense to me as if it was related to my question. I tried addint promise.all in front of the map and also adding await in front of the call to checking(), but it still doesn't work – Texas12 Mar 11 '23 at 20:01
  • @Pointy I take your point in the larger context, but from the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#description) *'Using `await` pauses the execution of its surrounding `async` function until the promise is settled...'* – pilchard Mar 11 '23 at 20:18
  • 1
    @pilchard I've seen that, and I consider it misleading. The word "pause" really is not appropriate, however I'm not sure what I would substitute. I really like `async` and `await` but it's hard to deny that it's made everything even more mysterious for newcomers to the language. – Pointy Mar 11 '23 at 20:20
  • I've tried this, which just returns false and still doesn't console log the dbPrice: var checkValidPrice = await Promise.all( req.body.products.map((item) => { const itemPrice = item.price; let dbPrice = Product.findById(item.productId, "price"); console.log("dbprice ", dbPrice.price); if (itemPrice === dbPrice.price) { return true; } else { return false; } }) ); console.log(`check price printing: ${checkValidPrice}`); – Texas12 Mar 11 '23 at 20:22

0 Answers0