i work nodejs express routes , mongoose persist data. did core routes crud operations no problem. however, when try perform operations on 1 of fields of model , try save model model.save says .save() method: "undefined not function"
so, here's code. snippet number (1) works fine:
router.put('/:myid', function (req, res, next) { var ourupdatedata = req.body; model.findone({myid: req.params.myid.tostring()}, function (err, foundmodel) { if (err) throw err; if (ourupdatedata.fielda) foundmodel.fielda = ourupdatedata.fielda; if (ourupdatedata.fieldb) foundmodel.fieldb = ourupdatedata.fieldb; if (ourupdatedata.fieldc) foundmodel.fieldc = ourupdatedata.fieldc; if (ourupdatedata.fieldd) foundmodel.fieldd = ourupdatedata.fieldd; if (typeof ourupdatedata.fieldarray === "object") ourupdatedata.fieldarray = ourupdatedata.fieldarray; foundmodel.save(function (err, updatedmodel) { if (err) throw err; res.send(updatedmodel); }); }); });
so model has 6 fields: fielda,..b,..c,..d, myid identify index , 1 field array of values fieldarray. example above saves model, works fine. if try array field fieldarray , save model throws me "undefined not function" when use model.save() . snippet (2) code produces error:
router.get('/:myid/:addthistofieldarray', function(req, res, next) { var myid = req.params.myid; var addthistofieldarray = req.params.addthistofieldarray; model.find({myid: myid}, function (err, model) { if (err) throw err; var fieldarray = model.fieldarray; fieldarray.push("new thing fieldarray"); var newfieldarray = fieldarray; if (typeof newfieldarray === "object") model.fieldarray = newfieldarray; model.save(function (err, updatedmodel){ if (err) throw err; res.send(updatedmodel); }); }); }); so thing above throws "undefined not function" on using model.save(.. )
i tried second variant of snippet (2), let's call snippet (3), incorporating .exec() doesn't work, throws same "undefined not function" on model.save(.. ) snippet (3) this:
router.get('/:myid/:addthistofieldarray', function(req, res, next) { var myid = req.params.myid; var addthistofieldarray = req.params.addthistofieldarray; model.find({myid: myid}).exec(function (err, model) { if (err) throw err; var fieldarray = model.fieldarray; fieldarray.push("new thing fieldarray"); var newfieldarray = fieldarray; if (typeof newfieldarray === "object") model.fieldarray = newfieldarray; model.save(function (err, updatedmodel){ if (err) throw err; res.send(updatedmodel); }); }); }); i'll greatful inputs , suggestions!
ansering attempt of willson:
yeah, know when call model.find(.. gives array, if call model.findone(.. gives 1 object json
i tried simplify example , in version actualy did use the: "model[0].fieldarray = newfieldarray" thing array fist (the array itself) , assign new value.
the problem still persists, gives me on model.save(.. "undefined not function " )
the current code is:
router.get('/:myid/:addthistofieldarray', function(req, res, next) { var myid = req.params.myid; var addthistofieldarray = req.params.addthistofieldarray; model.find({myid: myid}).exec(function (err, model) { if (err) throw err; var fieldarray = model[0].fieldarray; fieldarray.push("new thing fieldarray"); var newfieldarray = fieldarray; if (typeof newfieldarray === "object") model[0].fieldarray = newfieldarray; model.save(function (err, updatedmodel){ if (err) throw err; res.send(updatedmodel); }); }); }); this snippet (4) above gives on model.save(.. "undefined not function"
when use in mongoose find method, return array since discover 1 or many documents, in example querying 1 specific element id, should grab first element on returned array:
model.find({myid: myid}).exec(function (err, documents) { var model = documents[0]; if (err) throw err; var fieldarray = model[0].fieldarray; here example:
var mongoose = require('mongoose'); var schema = mongoose.schema; mongoose.connect('mongodb://localhost:27017/java-jedi'); var hackerschema = new schema({ name: string, languages: [string] }); var hacker = mongoose.model('hacker', hackerschema); // instantiating model. var onehacker = new hacker(); onehacker.name = 'richard stallman'; onehacker.save(function(err) { if (err) throw err; // finding document intentionally example hacker.find({_id: onehacker._id}, function(err, hackers) { var hacker = hackers[0]; // modifying document , updating it. hacker.languages.push('lisp'); hacker.save(function(err) { if (err) throw err; console.log(hacker); }); }); });
Comments
Post a Comment