繁体   English   中英

NOT在节点/猫鼬中不起作用

[英]NOT doesn't work in node/mongoose

我基本上只想在真与假之间切换。

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 }})

控制台始终记录为true。 只是不起作用。 在节点和猫鼬的世界中,我应该做不同的事情吗?

另外,现在是否可以只在mongodb / mongoose中输入一个表达式,而不是像这样进行解决?(甚至不起作用:/)

opposite的值将始终为true因为currentvalue.somethingTrue始终为undefined ,在JavaScript中为“ falsy ”值:

console.log( currentvalue.somethingTrue );  // undefined
console.log( !currentvalue.somethingTrue ); // true

而且,它是undefined因为从.findOne()返回的对象是Query实例,而不是从数据库中检索的文档。

.findOne()也异步操作(不阻止周围的代码继续运行),因此,对于它提供文档而言,它期望在文档可用(或发生err )时可以调用的callback函数:

doc.findOne({ _id : req.params.the_id }, function (err, currentvalue) {
    var opposite = !currentvalue.somethingTrue;
    // etc.
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM