比较 Mongoose 对象 ID 时,if 语句在 Jade 中不起作用

if statement doesn't work in Jade when comparing Mongoose Object Ids

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

//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

ObjecId 实例是对象,因此像对象一样进行比较,而不是基元。 您应该使用

post._id.equals(object._id)

post._id.toString() === object._id.toString()