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

Ste*_*hen 3 templates mongoose express 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 mongoose)

55e3e6dcd22670d8032a4ddf
55ef8999a89ed2fc72d8159f
no

55e3e6bbd22670d8032a4dde
55ef8999a89ed2fc72d8159f
no

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

55ef8028283872046809c0f2
55ef8999a89ed2fc72d8159f
no
Run Code Online (Sandbox Code Playgroud)

Ser*_*pin 5

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

post._id.equals(object._id)
Run Code Online (Sandbox Code Playgroud)

或者

post._id.toString() === object._id.toString()
Run Code Online (Sandbox Code Playgroud)