D
D
Dubolom Unicellular2020-06-17 11:50:26
JavaScript
Dubolom Unicellular, 2020-06-17 11:50:26

Why doesn't handlebars want to output data from MongoDB?

I did the usual stuff on Nodejs Handlebars and MongoDB.
But when I make a get request on index.hbs I am passing the following parameters from the database:

router.get("/", async (req, res) => {
  const todos = await Todo.find({});

  res.render("index", {
    title: "Todos List",
    isIndex: true,
    todos
  });
});


Handlebars in the console gave me an error:
Server has been started
Handlebars: Access has been denied to resolve the property "completed" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "title" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details

I went to the page that is indicated in the error ( https://handlebarsjs.com/api-reference/runtime-opt... )

But there, it shows the work with the handlebars package, and I have express-handlebars.
And in express-handlebars, there is no such place where this setting could be passed, I don’t even pass the result variable or something like that anywhere.

Then I just checked the todos array, (displayed in the console) I got:
[
  {
    completed: false,
    _id: 5ee932ae9c45385520bfb76f,
    title: 'Купить мозги',
    __v: 0
  }
]


And in index.hbs, I walked through the todos array and displayed the title and processed the presence of true in the completed property:
<h2>Todos page</h2>

{{#if todos.length}}
<ul>
  {{#each todos}}
  <li class="todo">
    <form action="/complete" method="POST">
      <label>
        {{#if completed}}
          <input type="checkbox" checked />
          <span class="completed">{{title}}</span>
        {{else}}
          <input type="checkbox" />
          <span>{{title}}</span>
        {{/if}}

        <button class="btn btn-small" type="submit">Save</button>
      </label>
    </form>
  </li>
  {{/each}}
</ul>
{{else}}
<p>No todos!</p>
{{/if}}


But in index.hbs nothing was given to me, even the name of the body.
Let me remind you that when I get a request for index.hbs, I get an error:

Server has been started
Handlebars: Access has been denied to resolve the property "completed" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
Handlebars: Access has been denied to resolve the property "title" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details

What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-06-17
@duboloms

try await Todo.find({}).lean()
mongoose queries return their mongoose objects with a bunch of additional properties, .lean() will return stupid json from the mongo base
https://mongoosejs.com/docs/tutorials/lean.html
I suspect that there mongoos adds some its .completed property

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question