Handlebars: Access has been denied to resolve the property “from” because it is not an “own property” of its parent

后端 未结 11 1828
我在风中等你
我在风中等你 2020-12-05 13:27

I am using a Nodejs backend with server-side rendering using handlebars. After reading a doc array of objects from handlebars, which contains key \"content\" an

11条回答
  •  眼角桃花
    2020-12-05 13:51

    Today I have the same warning from handlebars and the view is empty. Below is how I fixed that:

    //  * USERS PAGE
    // @description        users route
    // @returns           ../views/users.hbs
    router.get('/users', async (req, res) => {
      // get all items from db collection
      const collection = 'User'
      await dbFindAllDocs(collection) // <=> wrapper for Model.find() ...
        .then(documents => {
          // create context Object with 'usersDocuments' key
          const context = {
            usersDocuments: documents.map(document => {
              return {
                name: document.name,
                location: document.location
              }
            })
          }
          // rendering usersDocuments from context Object
          res.render('users', {
            usersDocuments: context.usersDocuments
          })
        })
        .catch(error => res.status(500).send(error))
    })
    

    the users.hbs file

      {{#each usersDocuments}}
    • name: {{this.name}} location: {{this.location}}
    • {{/each}}

    Creating an entire new Object named context with its own properties then pass in it into the render function will fix the issue...

    note:

    When we do not create a new Object, it is easy to accidentally expose confidential information, or information that could compromise the security of the projet, mapping the data that's returned from the database and passing only what's needed onto the view could be a good practice...

提交回复
热议问题