0

Can anyone help me where is the problem view is generated before mongoose get data from database. I already use await but its not waiting for response.

router.get('/', async(req, res, next)=> {
    try{
        const products = await Product.find({});
        res.render('shop/index', { title: 'Express',products });
    }catch(e){
        console.log(e);
    }

});
3
  • 1
    How did you conclude that it's not waiting for response ? Commented Dec 31, 2017 at 19:14
  • I need view render after get all Product information Commented Jan 1, 2018 at 9:10
  • Try logging products before rendering using console.log Commented Jan 1, 2018 at 14:30

2 Answers 2

1

Imao you tried to do somethins like this:

  router.get('/', async(req, res, next)=> {
      let products
      try{
          products = await Product.find({});
      }catch(e){
          console.log(e);
      }

      res.render('shop/index', { title: 'Express',products });
  });

But as I know the reason to use such syntax is cristmas tree of callbacks.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this

router.get('/', async(req, res, next)=> {
    try{
        Product.find({}).then(products => {
          res.render('shop/index', { title: 'Express',products });
        });
    }catch(e){
        console.log(e);
    }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.