Javascript 使用ShouldJS比较Mongoose中的数组
获取一个数组,如['hello','there'],并将其存储在Mongoose文档中,模式如下Javascript 使用ShouldJS比较Mongoose中的数组,javascript,node.js,mongoose,should.js,Javascript,Node.js,Mongoose,Should.js,获取一个数组,如['hello','there'],并将其存储在Mongoose文档中,模式如下 tags: { type: Array } 使用诸如: Something.create({ tags: ['hello', 'there']}, cb); 然后使用ShouldJS检查文档是否与我提供的数组匹配,我希望: doc.tags.should.eql(['hello', 'there']); 但事实并非如此。如果我得到console.log文档标记: [hello, there]
tags: { type: Array }
使用诸如:
Something.create({ tags: ['hello', 'there']}, cb);
然后使用ShouldJS检查文档是否与我提供的数组匹配,我希望:
doc.tags.should.eql(['hello', 'there']);
但事实并非如此。如果我得到console.log文档标记:
[hello, there]
请注意,引号已消失。doc.tags确实是一个数组(我可以检查数组的instanceof),我还可以使用shouldjs
doc.tags.should.have.keys('hello');
doc.tags.should.have.keys('there');
有人知道为什么我的数组不再匹配了吗?你的数组不是一个真正的json
array
:它是一个带有附加方法的mongoosarray
要使should.eql
与mongoose数组一起工作,请首先使用toObject()
:
愚蠢的问题,可能是一种类型,但当你输出时,你会得到[hello,there]或['hello','there'],因为它们不会相等,你会得到这个问题的答案吗?我也一样issue@dworrad-不,我从来没想过。
doc.tags.toObject().should.eql(['hello', 'there']);