简体   繁体   中英

Handlebar template access to mongo collection.find() variable in a nodejs

I have the nodejs express script as below

app.get('/approval',function(req,res){

        db.open(function(err,db) {
        var collection = db.collection('form');
    collection.find({ contact: 'James Wong' }).toArray(function(err, docs) {
        console.log(docs); 
   /*It will  will give an output something like
      { _id: 53533045fec60bd941c04a22,
       contact: 'James Wong'} */


        res.render('approval.handlebars',{resultfind : docs});
           /*it will give an output as [object Object]

        db.close();
      });

    });

My console.log(docs) output is in JSON ARRAY format which is correct. ie

{ _id: 53533045fec60bd941c04a22,
       contact: 'James Wong'} */

However when I display it in handlebartemplate by using res.render the result will be as below;

[object Object]

My template is below

<div>
{{resultfind}}
</div>

I wanted to access those object properties.

Got it resolved ..Thanks to TheShellfishMeme .

Two things to do

1) instead of toArray function , replace it by each function . This is to maintain json format

2) to access the JSON format at the html {{resulfind.contact}}

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM