Express 및 Monk를 사용하여 MongoDB 어레이를 반복하는 방법 각 요소에 대한 페이지를 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com//questions/25084422

  •  02-01-2020
  •  | 
  •  

문제

나는 Express, NodeJS 및 Monk가있는 웹 앱을 구축하고 있습니다.MongoDB 데이터베이스에서 배열의 각 요소에 대해 페이지를 만들려고합니다.

데이터가 coll_list 와 같이 coll_list 와 같은 컬렉션 이라는 콜렉션에 이미 있음 :

{ "_id" : ObjectId("53dbaefd3d85d57492506f1f"), "coll_list" : [     "data_pagename1", 
     "data_pagename2", "data_pagename3" ] }
.

coll_list 에서 의 모든 요소를 반복 할 수 있다고 생각했습니다.

 router.get('/index', function(req, res) {
     var db = req.db;
     var collection = db.get('collections');
     collection.find( "coll_list" , function(e,docs) {
       for (elems in docs) {
         res.render(elems, {
           elems : docs
         });
       }
     });
 });
.

어떤 제안이나 도움이나이를 수행하는 방법에 대한 도움이 크게 감사 할 것입니다.

도움이 되었습니까?

해결책

Req.Params

router.get('/coll/:id',
  function(req,res){
     //access the id by req.params.id
     //req.params.id will essentially be the _id of the document
     //use it to obtain data from mongoDB and render the page using that data
     //From the front end you make the call to /coll/<ObjectId> like
     // /coll/53dbaefd3d85d57492506f1f and you get that id in req.params.id and use it to
     //render data specific to that _id. 

  });
.

이렇게 단일 경로를 사용하면 coll_list

의 모든 항목에 대한 페이지를 만들 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top