Templates if语句不';在比较猫鼬对象ID时,Jade中不起作用

Templates if语句不';在比较猫鼬对象ID时,Jade中不起作用,templates,express,mongoose,pug,Templates,Express,Mongoose,Pug,所以我有一个故事对象,我试图将它与一个故事数组进行比较,看看这个故事对象是否在数组中。但是它不起作用,我不知道为什么 //news.jade each post in favPosts div=post._id div=object._id if (post._id === object._id) span same else span no // the output (those are ObjectID from mong

所以我有一个故事对象,我试图将它与一个故事数组进行比较,看看这个故事对象是否在数组中。但是它不起作用,我不知道为什么

//news.jade

each post in favPosts
    div=post._id
    div=object._id
    if (post._id === object._id)
        span same
    else
        span no

// the output (those are ObjectID from mongoose)

55e3e6dcd22670d8032a4ddf
55ef8999a89ed2fc72d8159f
no

55e3e6bbd22670d8032a4dde
55ef8999a89ed2fc72d8159f
no

55ef8999a89ed2fc72d8159f
55ef8999a89ed2fc72d8159f
no <=== this should be same

55ef8028283872046809c0f2
55ef8999a89ed2fc72d8159f
no
//news.jade
每一个帖子都是favPosts
div=邮政编码。\u id
div=对象。\u id
if(post.\u id==object.\u id)
跨度相同
其他的
跨度号
//输出(来自mongoose的ObjectID)
55e3e6dcd22670d8032a4ddf
55ef8999a89ed2fc72d8159f
不
55E3E6BBD22670D8032A4DE
55ef8999a89ed2fc72d8159f
不
55ef8999a89ed2fc72d8159f
55ef8999a89ed2fc72d8159f

ObjecId
实例是对象,因此与对象(而非原语)类似进行比较。 你应该使用其中一种

post._id.equals(object._id)


您可以尝试将这两种类型强制转换为mongo db对象ID吗?您可能需要使用
=
而不是
==
,尽管它们似乎应该是相同的类型,因此不需要。此外,您可能需要考虑一种更有效的比较检查方法,这取决于您如何使用它。(即:找到一篇具有特定id的文章。如果你打算对所有文章都做些什么,那么这可能无关紧要,你可以忽略最后一点。)你能试试
div=typeof post.\u id
div=typeof object.\u id
看看它们的类型吗?
post._id.toString() === object._id.toString()