mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

CoreMongooseArray.slice strips `Symbol(mongoose#Array#_schema)` property from returned CoreMongooseArray

Open hyperparabolic opened this issue 6 years ago • 5 comments

Do you want to request a feature or report a bug? Bug report?

What is the current behavior? CoreMongooseArray's slice() function returns a CoreMongooseArray without a Symbol(mongoose#Array#_schema) property. This results in the following error message if you follow it with a unshift(..), splice(...) or nonAtomicPush(...):

.../node_modules/mongoose/lib/types/core_array.js:258
    return this[arraySchemaSymbol].caster.applySetters(value, this[arrayParentSymbol], false);
                                   ^

TypeError: Cannot read property 'caster' of undefined
    at CoreMongooseArray._cast (.../node_modules/mongoose/lib/types/core_array.js:258:36)
    at CoreMongooseArray.splice (.../node_modules/mongoose/lib/types/core_array.js:815:16)
    at Object.<anonymous> (.../test.js:20:3)
    at Module._compile (internal/modules/cjs/loader.js:698:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

This may be related to https://github.com/Automattic/mongoose/issues/8356, as using Array.from(x.a).slice() is a serviceable workaround for me right now, but it still seems like an unintended behavior.

If the current behavior is a bug, please provide the steps to reproduce. Reproducing snippet: https://gist.github.com/hyperparabolic/ac1ae6032f283b64c7768b156f542e11

What is the expected behavior? I would expect to be able to call unshift() or splice() on arrays returned by slice()

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Mongoose: 5.8.5 Node: 10.17.0 No running db required.

hyperparabolic avatar Jan 06 '20 22:01 hyperparabolic

It looks like the 5.8.6 release sneaked out right after I posted this. I can confirm it's still an issue on that version as well.

hyperparabolic avatar Jan 09 '20 17:01 hyperparabolic

Did this regress? My repro script is failing on 5.9.2.

hyperparabolic avatar Feb 25 '20 19:02 hyperparabolic

@hyperparabolic I can confirm this did not regress, we have a test case for this and it is passing: https://github.com/Automattic/mongoose/commit/a3ab3a65d361eff6e39ef79dcdbd3ac7a11de5b1 .

Can you please post your repro script?

vkarpov15 avatar Feb 28 '20 15:02 vkarpov15

Splice did get fixed, but unshift is still showing the same behavior as before.

Here is a second test for below that snippet https://github.com/Automattic/mongoose/commit/a3ab3a65d361eff6e39ef79dcdbd3ac7a11de5b1:

  it('copies schema correctly (gh-8482)(part 2)', function() {
      const M = db.model('Test', Schema({ arr: [Number] }));

      const doc = new M({ arr: [1, 2, 3] });

      const arr = doc.arr.slice(2);

      arr.unshift(10);

      assert.deepEqual(arr, [10,3]);
    });

Result of running that one test:

  !

  0 passing (294ms)
  1 failing

  1) types array
       slice
         copies schema correctly (gh-8482)(part 2):
     TypeError: Cannot read property 'caster' of undefined
      at CoreMongooseArray._cast (lib/types/core_array.js:258:36)
      at Arguments.map (<anonymous>)
      at CoreMongooseArray.unshift (lib/types/core_array.js:877:25)
      at Context.<anonymous> (test/types.array.test.js:1676:11)
      at processImmediate (internal/timers.js:439:21)

hyperparabolic avatar Feb 29 '20 01:02 hyperparabolic

Thanks!

hyperparabolic avatar Mar 07 '20 23:03 hyperparabolic