Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am posting what fixed my casting to objectId. It is the story of updatedBy key of my features document. I added some comments to show you the problematic area corresponds to the casting issue. </p> <p>import mongoose;</p> <pre><code> var mongoose = require('mongoose'); </code></pre> <p>define objectId;</p> <pre><code> var ObjectId = mongoose.Schema.Types.ObjectId; </code></pre> <p>define the type in your schema;</p> <pre><code> var featureSchema = new Schema({ name: String, isSystem: { type: Number, min: 0, max: 1 }, updatedBy: {type:ObjectId, ref:'users'}, //here I defined the field! updatedAt: { type: Date, default: Date.now } }); </code></pre> <p>and then insert the document via post method;</p> <pre><code> app.post('/features', function (req, res){ var feature; console.log("POST: "); console.log(req.body); feature = new Feature({ name: req.body.name, isSystem: req.body.isSystem, updatedBy: req.body.updatedBy, //here I insert the objectId field updatedAt: new Date() }); feature.save(function (err) { if (!err) { return console.log("created"); } else { return console.log(err); } }); return res.json({ features: feature }); }); </code></pre> <p>and finally this is your json string. you can fire it from google chrome javascript console or a similar one;</p> <pre><code> jQuery.post("/features", { "name": "Receipt", "isSystem": 0, "updatedBy": "528f3d44afcb60d90a000001" },function (data, textStatus, jqXHR) { console.log("Post resposne:"); console.dir(data); console.log(textStatus); console.dir(jqXHR); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload