【问题标题】:Comparing arrays from Mongoose using ShouldJS使用 ShouldJS 比较来自 Mongoose 的数组
【发布时间】:2013-05-25 03:51:28
【问题描述】:

获取诸如 ['hello', 'there'] 之类的数组并将其存储在具有诸如

之类的架构的 Mongoose 文档中
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');

有人知道为什么我的数组不再匹配了吗?

【问题讨论】:

  • 愚蠢的问题,可能是一种类型,但是当您输出时,您会得到 [hello, there] 或 ['hello', 'there'] 因为它们不相等
  • 你有没有得到这个问题的答案?我也有同样的问题
  • @dworrad - 不,从来没想过。

标签: javascript node.js mongoose should.js


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2020-06-15
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多