1

I'm trying to do a very simple app 'todo app' on koajs with swig and I have succeeded in inserting data to Mongodb, in my case the name of the db is 'mytodos' and the collection is 'todos'.

How can I retrieve the '_id' of a document? I tried doing this:

index.html (this lists the todo items)

<div class="todoListClass">
  <p>
    <strong>Your todo list:</strong> {{todos.length}}
    {% for todo in todos%}
    <li>{{todo.text1}} <span>
    <a href="/todo/delete/{{todo._id}}">Delete</a></span></li>
    {%endfor%}
  </p>
</div>

but I can't seem to get the result that I want. It just returns '[Object][object]'.

1 Answer 1

1

The _id field of your todo item is loaded from mongodb for displaying the _id field which is of type ObjectId you will need to get a string representation by calling _id.toString().

<div class="todoListClass">
  <p>
    <strong>Your todo list:</strong> {{todos.length}}
    {% for todo in todos%}
    <li>{{todo.text1}} <span>
    <a href="/todo/delete/{{todo._id.toString()}}">Delete</a></span></li>
    {%endfor%}
  </p>
</div>
1
  • awesome! that fixed it! Thanks!
    – ickyrr
    Jan 19, 2016 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.