Reputation: 147
I basically just want to toggle something between true and false.
var currentvalue = doc.findOne({ _id : req.params.the_id });
var opposite = !currentvalue.somethingTrue;
console.log("will this work?? " + opposite);
doc.update({ _id : req.params.the_id }, { $set : { somethingTrue : opposite }})
the console always logs true. it just doesn't work. in the world of node and mongoose, should I do this different?
Also, is it possible now to just enter an expression in mongodb/mongoose, instead of having a workaround like this?(which doesn't even work :/ )
Upvotes: 0
Views: 27
Reputation: 123513
The value of opposite
will always be true
because currentvalue.somethingTrue
will always be undefined
, which is a "falsy" value in JavaScript:
console.log( currentvalue.somethingTrue ); // undefined
console.log( !currentvalue.somethingTrue ); // true
And, it's undefined
because the object returned from .findOne()
is a Query
instance rather than the document being retrieved from the database.
.findOne()
also acts asynchronously (without blocking surrounding code from continuing), so for it to provide the document, it expects a callback
function that it can invoke when the document has become available (or an err
has occurred):
doc.findOne({ _id : req.params.the_id }, function (err, currentvalue) {
var opposite = !currentvalue.somethingTrue;
// etc.
});
Upvotes: 1