2017-12-31 265 views
0

mongooseがデータベースからデータを取得する前に、問題のビューがどこに生成されるかを教えてください。私はすでに待っていますが、応答を待っていません。私は、このような構文を使用する理由を知っているのcristmasツリーでExpress JS mongoDB async await

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

しかし:

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

}); 
+1

あなたはそれが応答を待っていないとどう結論しましたか? – Malice

+0

私はすべてを取得した後でレンダリングが必要製品情報 –

+0

レンダリングの前に 'console.log'を使って製品をログに記録してみてください。 – Malice

答えて

0

あなたはこの

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

}); 
1

イマオようにそれを行うことができますが、あなたのようsomethinsを実行しようとしましたコールバック。

+1

おかげさまで、ありがとうございます。ハッピーコーディング!!!! –