express - Mongoose not $setting array.$.booleanValue when I findOneAndUpdate({paramOne: p, paramTwo, array.keyName: valueName}) -
i'm trying update boolean value in object that's in array in mongo document mongoose through
app.post("/api/post/:chorecomplete", function(req, res){ parent.findoneandupdate({parentfirstname: req.body.parentfirstname, parentlastname: req.body.parentlastname, "chores.chorename": "firstchore"}, {$set: {"chores.$.complete": "true"}}).exec(function(err, doc){ if (err){ console.log(err); res.send("not ok");} else{ console.log(doc);`enter code here` res.send(ok); } }) }) }
my server logs mongoose response, not because i'm telling too. commented out console.log(doc) , tested see if db returning or server logging it. thing is, server responds "ok" meaning mongoose not throwing error, isn't modifying post /api/post/chorecomplete 200 17.378 ms - 2 [1] { _id: 597aa48313cf6914905916e0, [1] parentfirstname: 'firstname', [1] parentlastname: 'lastname', [1] parentemail: 'mich@ael.com', [1] __v: 0, [1] children: [], [1] chores: [1] [ { chorename: 'firstchore', [1] choredesc: 'firstdescription', [1] value: null, [1] complete: false }, [1] { chorename: 'secondchore', [1] choredesc: 'seconddescription', [1] chorevalue: null, [1] complete: false }] }
i don't know i'm doing wrong. i've got static query parameter in there, but"firstchore" req.body.chorename. i'm beginning wonder if should make collection chores , ref parent , child schemas, i'm not 100% sure how go implementing reference parent , child ref each other... (parent , child both have separate user accounts, child update chore completed , receive chorevalue [which supposed $5.00 in example, reason mongoose doesn't accept it. it's not defined anywhere except form values.]). example schema. (i'm trying detailed possible. it's late , first question here.)
```//parent schema var mongoose = require("mongoose"); var schema = mongoose.schema;
var parentschema = new schema({ parentfirstname: { type: string, required: true },
parentlastname: { type: string, required: true }, parentemail: { type: string, required: true, validate: [ function(input){ input.length >= 3; }, "email must valid email" ] }, password: { type: string, //required: true, validate: [ function(input){ input.length >= 6; }, "password must @ least 6 characters" ] }, chores: { type: array, }, children: [{ type: schema.types.objectid, ref: "child" }]
});
var parent = mongoose.model("parent", parentschema);
module.exports = parent;
```
this route adding chore array. form values. chore defined on fly in route opposed explicitly defined it's own model. //route inserting chores app.post("/api/post/:chores", function(req, res){ //when have logged in take 1 of values presence, (either _id or email) , replace name. it's name b/c name inserted db. //if chores === chores findall else if {var thechoretofind === req.params.chores} , we'll run chore update chore? var parentfirstname = req.body.parentfirstname; var parentlastname = req.body.parentlastname; var chorename = req.body.chorename; var choredesc = req.body.choredesc; var chorevalue = req.body.chorevalue; parent.findoneandupdate({parentfirstname: req.body.parentfirstname, parentlastname: req.body.parentlastname}, {$push: {chores: {chorename: chorename, choredesc: choredesc, chorevalue: chorevalue, complete: false}}}).exec(function(err, doc){ if(err) {console.log(err)} console.log(doc); }) res.send("ok"); })//end new chores
this incredibly long winded. didn't want have brought not here reference. if you've made far. thank you.
possible issue
this looks may issue express routes rather mongodb.
the rest of code looks valid, have 1 post
route named /api/post/:chores
, route name /api/post/:chorecomplete
though parameter names different express interpret these routes identical since colon makes parameter wildcard can value pass it.
in express, order in properties attached app
important.
in situation, assuming don't have other post
routes match /api/post/:parameter
pattern, if /api/post/:chores
written before /api/post/:chorecomplete
post request /api/post/:chorecomplete
match /api/post/:chores
, run code have in route.
this reason you're getting ok
response , console logging document db, because you're posting /api/post/:chores
and running successful query.
solution
there few ways address this, revolve around changing path of route. depends on naming pattern , how plan access data.
since code in question updating existing document make put
route instead
app.put("/api/post/:chorecomplete", function(req, res) {...}
or keep post
, append name path no longer matches route in code:
app.post("/api/post/chorecomplete/:parameter", function(req, res) {...}
Comments
Post a Comment