在比较mongoose对象ID时,如果语句在jade中不起作用

b5lpy0ml  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(201)

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

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

q0qdq0h21#

ObjecId 示例是对象,因此比较起来像对象,而不是基本体。你应该使用其中一种

post._id.equals(object._id)

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

相关问题