javascriptnode.jsexpressswig-template

Node, Express, Swig Template - Variable in HTML attribute


I'm using http://paularmstrong.github.io/swig/ 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 %}

The people object is ok, and the loop works. However you'll notice that I'm trying to use person._id 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 person._id within a HTML attribute, then get the correct value I'm after when using person._id inside HTML tags..?


Solution

  • 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.