使用ShouldJS比较Mongoose的数组

时间:2013-05-25 03:51:28

标签: 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 doc标签:

[hello, there] 

请注意,引号已经消失。 doc.tags确实是一个数组(我可以检查instanceof数组),我也可以使用带有

的应用程序
doc.tags.should.have.keys('hello');
doc.tags.should.have.keys('there');

任何人都知道为什么我的阵列不再匹配了?

1 个答案:

答案 0 :(得分:3)

你的数组不是真正的json Array:它是MongooseArray,还有其他方法。

要使should.eql与mongoose数组一起使用,请先使用toObject()

doc.tags.toObject().should.eql(['hello', 'there']);