Stephen Last Stephen Last - 1 year ago 234
Node.js Question

Node, Express, Swig Template - Variable in HTML attribute

I'm using as my template engine for a Node/Express app.

I can pass data into my template fine, but I'm having trouble using variables.

I have:

{% for person in people %}
<input type="radio" name="id" value="{{ person._id }}" id="id_123" /> <label for="id_123">{{ 'id = ' + person._id }}</label><br />
{% endfor %}

object is ok, and the loop works. However you'll notice that I'm trying to use
twice - the first one doesn't work, the second one does.

value="{{ person._id }}"
// value="[object Object]"

<label for="id_123">{{ 'id = ' + person._id }}</label>
// <label for="id_123">id = 568bc95f317e30e813541ab0</label>

Why do I get
[object Object]
when trying to use
within a HTML attribute, then get the correct value I'm after when using
inside HTML tags..?

Answer Source

Right, the person._id is actually a Mongoose ObjectId, which now makes sense to me. However when I console.log it, it shows a string.

Anyway, just doing '' + person._id works for what I need.