0

I am trying to create a "show" page to present more Infrmation about an Item from MongoDB. I would like to pass a Javascript Object with a specific id into my Vue Instance. However it renders an invalid Object:

  _doc: {
    name: 'Dr. Florent Ratatouille',
    _id: ObjectID { _bsontype: 'ObjectID', id: '^ÚlV4QºÄ;ù' }
  }

The client then throws the error in the last line:
Uncaught SyntaxError: Unexpected token '{'.
The _id property is not declared by me but by mongodb. The backend looks like this:

app.get("/docs/:id", function(req, res){
    doc.findById(req.params.id).populate("comments").exec(function(err, foundDoc){
        if(err){
            console.log(err);
        } else {
            console.log(foundDoc)
            res.render("docs/show2", {doc: foundDoc});
        }
    });
});

Have you any Ideas whats going on? Am I missing something obvious? Any help is greatly appreciated!

TopDocDev
  • 15
  • 4
  • Are you trying to return the document as JSON to a client? Is that error on the client? Is this useful to you? https://stackoverflow.com/questions/16586180/typeerror-objectid-is-not-json-serializable – Chase Jun 05 '20 at 17:41
  • Thanks for the answer, the object appears to be a "Vanilla" Js, not JSON. Yes the error is on the client. The tlink discusses another problem, but i appreciate your Input! – TopDocDev Jun 05 '20 at 18:29
  • Our (my group) typical approach to this is to toString() any ObjectId on records that are being returned as json data. Javascript (or JSON for that matter) has no idea what an ObjectId is. – Taplar Jun 05 '20 at 18:29

0 Answers0