object.valueOf()if return false

时间:2016-01-02 16:20:12

标签: javascript mongodb

我感到困惑的是,如果JavaScript中的类如何工作,我总是知道它稍微复杂一点,但现在我只是不知道。代码和输出如下。

以下json存储在mongodb中。我怀疑这可能是错误的地方..

的console.log(JSON.stringify(用户)):

{"_id":"5687f787f8ad41175fab5bd5","pic":"karl.png","language":"5687f787f8ad41175fab5bd2","cell":1,"local":{"email":"karl.morrison@email.com","password":"12345"},"profile":{"name":"Karl Morrison"},"sessions":[{"id":"5687f79bf8ad41175fab5bd9","seen":false,"active":false}]}

的console.log(JSON.stringify(消息)):

{"authorId":"5687f787f8ad41175fab5bd5","upvotes":0,"created":"2016-01-02T16:15:23.621Z","message":"<p>aa</p>"}

泽代码:

console.log('"' + user._id.valueOf() + '" "' + message.authorId.valueOf() + '"');

console.log({}.toString.call(user._id).split(' ')[1].slice(0, -1).toLowerCase());
console.log({}.toString.call(message.authorId).split(' ')[1].slice(0, -1).toLowerCase());

if (user._id.valueOf() == message.authorId.valueOf()) {
    console.log('TRUE');
} else {
    console.log('FALSE');
}

控制台:

"5687f787f8ad41175fab5bd5" "5687f787f8ad41175fab5bd5"
object
object
FALSE

我不明白为什么没有归还?

1 个答案:

答案 0 :(得分:0)

根据mongodb documentation

  

在2.2版中更改:在以前的版本中ObjectId.valueOf()   返回ObjectId()对象。

由于rect { fill: transparent; shape-rendering: crispEdges; } .axis path, .axis line { fill: none; stroke: rgba(0, 0, 0, 0.1); shape-rendering: crispEdges; } .axisLine { fill: none; shape-rendering: crispEdges; stroke: rgba(0, 0, 0, 0.5); stroke-width: 2px; } .dot { fill-opacity: .5; } .d3-tip { line-height: 1; font-weight: bold; padding: 12px; background: rgba(0, 0, 0, 0.8); color: #fff; border-radius: 2px; } /* Creates a small triangle extender for the tooltip */ .d3-tip:after { box-sizing: border-box; display: inline; font-size: 10px; width: 100%; line-height: 1; color: rgba(0, 0, 0, 0.8); content: "\25BC"; position: absolute; text-align: center; } /* Style northward tooltips differently */ .d3-tip.n:after { margin: -1px 0 0 0; top: 100%; left: 0; } user._idmessage.authorId个对象,常规ObjectId会比较他们的参考文献,因此您获得==

尝试使用:

  • false
  • user._id.toString() == message.authorId.toString()
  • 升级到更新的mongodb驱动程序。