Array stored in Mongo fails deep assert comparison to native javascript array with same length and values
I have a field in m开发者_运维技巧ongo defined with the mongoose ORM like so:
state: {type: [Number], required: true }
If I take a peek at a sample document with the mongo console, state looks like
state: [ 1, 1, 1 ]
So far, so good. But strange enough for that same document the following assert fails:
assert.deepEqual state, [ 1, 1, 1 ]
Can't figure out if this is something I'm missing with object comparisons in JS or something to do with the way mongo is returning the state array.
MongoDB has a bug where properties that should be non-enumerable are enumerated:
Eg, an array whose values are:
[ '0', '1']
has the following keys, according to Object.keys()
:
[ '0', '1', '_atomics', 'validators', '_path', '_parent', '_schema' ]
Note Mongo now uses V8, which supports ES5, which has had the ability to create non-enumerable properties via Object.defineProperty() for many, many years.
As the other poster mentions:
var fixMongoArray = function(array) {
return Array.prototype.slice.call(array)
}
精彩评论