Reputation: 4956
We are using Strongloop's Loopback framework to expose some REST APIs. We have different loopback models corresponding to the different REST entities. In loopback model A, we return an array in a API response. In a different loopback model, model B, we are using the SortedSet collection exposed by CollectionsJS like so:
var SortedSet = require('collections/sorted-set'); //Start of the file
...
...
//create the SortedSet instance and push values.
Now, without this require, model A's API returns the array correctly. After adding this require in model B, model A's API returns an object instead of array (keys are array indexes, values are array values). Model A code remains untouched.
So my guess is that there is some sort of clash wherein Loopback and/or NodeJS use a type that is used/manipulated in CollectionsJS as well. But how do I even debug this issue?
Upvotes: 0
Views: 75
Reputation: 4956
Found it! CollectionsJS "shims" certain additional methods onto the Array prototype, one of which is "toObject". Loopback calls the .toObject method on all the model attributes during response generation. One of the model attributes is an array, which means .toObject is called on it as well. Now, if I don't "require" the collections module, Array prototype does not get injected with toObject method, so the usual array representation is used by Loopback.
Upvotes: 0