Tuesday, 30 May 2017

How to return everything from a subdocument in MongoDB using Mongoose

I have a document in MongoDB that looks like

db.items.find({name:”NAME”}).pretty()
{
  "_id" : ObjectId("592486c7df778706f7bedb39"),
  "name" : “NAME”,
  "subheading" : “subheading”,
  "bannerImagePath" : "img/all/image_b39.jpg",
  "description" : “desc”,
  "Button" : {
      item-name=“ITEM NAME”
      item-price="200.00"
      item-id=“1”
      item-url="https://example.com/product"
},

and a route

router.get(‘/somepage’, function (req, res, next) {
Item.find({"_id": ("592486c7df778706f7bedb39")}).findOne(function(err, docs) {
res.render(‘folder/somepage’,{title:’TITLE’, items:docs});

That renders each of the top level fields using handlebars -

<p class="paragraph paragraph_100”></p>

I want to get everything inside the "Button" field to render on the page using something like

<p class="paragraph paragraph_100”></p>

But I need it to display in the view, inside a button exactly as -

  item-name="ITEM NAME"
  item-price="200.00"
  item-id="1"
  item-url="https://example.com/product"

If I put all the required fields at the top level of the schema (inside MongoDB) -

dataName= "ITEM NAME"
dataPrice= "200"
dataId= "1"
dataUrl="https://example.com/product"

Then in the view -

item-name= 
item-price= 
item-id= 
item-url= 

then it works, but I'd like to wrap all of that inside one set of braces and just call .. How would I do this?



via Michael

No comments:

Post a Comment