Wie kann man mit Express und Monk ein Mongodb-Array durchlaufen, um für jedes Element eine Seite zu erstellen?

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

  •  02-01-2020
  •  | 
  •  

Frage

Ich baue eine Web-App mit Express, Nodejs und Monk.Ich versuche, für jedes Element eines Arrays in meiner Mongodb-Datenbank eine Seite zu erstellen.

Diese Daten befinden sich bereits in der Sammlung namens Sammlung mit dem Schlüssel coll_liste wie so:

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

Ich dachte, es könnte möglich sein, alle Elemente in einer Schleife zu durchlaufen coll_liste mit so etwas wie:

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

Vorschläge oder Hilfe / Hinweise dazu, wie dies zu tun ist, wären sehr dankbar.

War es hilfreich?

Lösung

Verwenden erf.parameter

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. 

  });

Mit einer einzigen Route könnten Sie also für jedes Element in eine Seite erstellen coll_list

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top