2017-12-31 174 views
0

mongoose가 데이터베이스에서 데이터를 가져 오기 전에 문제 뷰가 생성 된 곳을 도와 줄 수 있습니까? 나는 이미 기다리고 있지만 반응을 기다리지 않고 기다리고있다. 나는 그런 구문을 사용하는 이유를 알고의 cristmas 트리입니다Express JS mongoDB 비동기 대기

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

Imao처럼 할 수는 다음과 같이 somethins을하려고 노력 콜백.

+1

당신의 도움에 감사드립니다. 해피 코딩 !!!! –