1

I have this Handlebar code in an Express project:

{{#if user}}
<p>Welcome, <a href="/profile/{{user.[_id]}}">{{user.username}}</a>.</p>
{{#each user.snippets}}
{{log 'heading' this.heading}}
<h1>{{heading}}</h1>
<code>{{code}}</code>
{{/each}}

The username is output correctly, and the loop is executed the right amount of times (which I see from the logging). However, the string properties heading and code are undefined, even though they are correctly set in my database. Both user and snippets are Mongoose objects or schemas (I am not sure about the terminology), or rather snippets (a property of users) is an array of such objects.

I am not a 100% sure if I'm using the this keyword correctly, or if I need brackets. I have also tried helpers, but they don't seem to do what I want. Any suggestions?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Dominus
  • 63
  • 8
  • I would be curious to know what each `snippet` object looks like. I would try `{{log this}}` within the `{{#each user.snippets}}` to make sure each `snippet` object has the shape you expect. – 76484 Jun 21 '22 at 03:03
  • @76484 Oh, good idea. I haven't used the logger as much as in Javascript, I'm afraid. In fact, I only used the line shown in the code above. Logging the snippet object gives me 'new ObjectId("[hexadecimal string]")'. I know this is the _id of Mongoose objects. I tried logging user, which shows a more natural object. I checked the database (I use MongoDB Atlas, for reference), and it does seem as if the snippets property of users is, indeed, an array of those ObjectIds. This is probably a fault with my JS code. I probably don't initialise each array index correctly. – Dominus Jun 21 '22 at 08:04
  • @76484 Yeah, I have probably pushed the snippet object to the snippets array wrong. Like this: user.snippets.push(snippet). You wouldn't by any chance know how to correctly do it. I am trying to do it like user.updateOne({ $push: {snippets: snippet} }), but it doesn't work. – Dominus Jun 21 '22 at 10:43
  • Perhaps this post can help you: https://stackoverflow.com/a/33049923/3397771 – 76484 Jun 21 '22 at 10:51
  • @76484 Thanks, but I asked a new question, where the problem is more specified. – Dominus Jun 21 '22 at 11:25

0 Answers0