LoopbackJS 的 collectionsJS 模块有问题

LoopbackJS having problems with the collectionsJS module

我们正在使用 Strongloop 的环回框架公开一些 REST API。我们有不同的环回模型对应于不同的 REST 实体。在环回模型 A 中,我们在 API 响应中 return 一个数组。在不同的环回模型中,模型 B,我们正在使用 CollectionsJS 公开的 SortedSet collection,如下所示:

var SortedSet = require('collections/sorted-set'); //Start of the file
...
...
//create the SortedSet instance and push values.

现在,没有这个要求,模型 A 的 API return 数组正确。在模型 B 中添加此 require 后,模型 A 的 API return 是一个对象而不是数组(键是数组索引,值是数组值)。模型 A 代码保持不变。

所以我的猜测是存在某种冲突,其中 Loopback and/or NodeJS 在 CollectionsJS 中也使用了 used/manipulated 的类型。但是我该如何调试这个问题呢?

找到了! CollectionsJS "shims" Array 原型上的某些附加方法,其中之一是 "toObject"。 Loopback 在响应生成期间对所有模型属性调用 .toObject 方法。模型属性之一是数组,这意味着 .toObject 也会在其上调用。现在,如果我不 "require" 集合模块,数组原型不会注入 toObject 方法,因此 Loopback 使用通常的数组表示。