2

Mind if I ask for some advice on how to loop through the follow JSON objects please and put it into a Handlebars template?

I've found a lot of similar questions here for "Handlebars and looping through JSON", but after quite a few hours of reading & trying various permuations, I'm pretty stuck.

The resorts object appears on the template page ok, if I print {{resorts}}.

And I can get a bullet list that seems to correspond with the number of documents within resorts.

But my goal is to get a bulleted list of each properties Name, and I've not been able to do that.

Many thanks in advance for any help.

My JSON:

{ properties: { 
      Country: 'United States', 
      Name: 'Killington', 
      description: `blurb` 
  }, 
  geometry: { 
      coordinates: [ -72.8032981, 43.6176027 ], 
      type: 'Point' 
    }, 
  datePosted: 2020-01-10T12:07:00.340Z, 
  _id: 5e11cd71746ed55b54a760ec, 
  type: 'Feature' 
},
{ properties: { 
      Country: 'United States', 
      Name: 'Jay', 
      description: `blurb` 
  }, 
  geometry: { 
      coordinates: [ -72.8032981, 43.6176027 ], 
      type: 'Point' 
    }, 
  datePosted: 2020-01-10T12:07:00.340Z, 
  _id: 5e11cd71746ed55b54a760ec, 
  type: 'Feature' 
}

My MongoDB Model (resort.js)

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const ResortSchema = new Schema ({
  type: String,
  properties:{
    Country: String,
    Name: String,
    description: String
  },
  geometry: {
    type: {
      type: String, 
      enum: ['Point'],
      required: true
    },
    coordinates: {
      type: [Number],
      required: true
    }
  },
  datePosted: {
    type: Date,
    default: new Date()
  },
  image: String
})


const Resort = mongoose.model('Resort', ResortSchema);

module.exports = Resort

My JS

exports.home = async(req, res) => {
  const Resort = require('../models/resort')
  const resorts = await Resort.find({})
  res.render('home', {resorts: resorts})
}

My Handlebars Template:

{{#if resorts}}
<ul>
  {{#each resorts}}
  <li>
    {{properties.Name}}
  </li>
  {{/each}}
</ul>
{{/if}}

Edit: I get a JSON output if I do the following, but still can't succeed if I try to drill down to properties.Name:

<ul>
  {{#resorts}}
      <li>
        {{this}}
      </li>
  {{/resorts}}
</ul>

Output

{ properties: { Country: 'USA', Name: 'Killington', description: "blurb" }, geometry: { coordinates: [ -72.8032981, 43.6176027 ], type: 'Point' }, datePosted: 2020-01-10T12:07:00.340Z, _id: 5e11cd71746ed55b54a760ec, type: 'Feature' }

UPDATE

Currently trying to create JSON validation in MongoDB. Could have something to do with that.

christo
  • 127
  • 1
  • 2
  • 12
  • 1
    Have you tried using `this` as `{{this.properties.Name}}`? – chridam Jan 10 '20 at 15:25
  • Thanks for the suggestion @chridam. That gets me the bullets. But unfortunately the name doesn't come through. I was also unsuccessful with other similar variants, such as {{ this.Name}} or {{Name}}. – christo Jan 10 '20 at 21:02

1 Answers1

-1
<ul>
  {{#each resorts}}
    {{#each this}}
      <li>{{name}}</li>
    {{/each}}
  {{/each}}
</ul>

or

<ul>
  {{#each resorts}}
    {{#each this}}
      <li>{{this.name}}</li>
    {{/each}}
  {{/each}}
</ul>
Scott Swezey
  • 2,147
  • 2
  • 18
  • 28