Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.populate() throws when populating immutable array of refs with String IDs #10264

Closed
ThoseGrapefruits opened this issue May 18, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ThoseGrapefruits
Copy link

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

Report a bug, possibly related or at least adjacent to #10159.

What is the current behavior?

When trying to populate an array of immutable refs, whose reference ID type is String, mongoose throws the following error:

/Users/l/Desktop/node_modules/mongoose/lib/schema/array.js:392
        throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
              ^

CastError: Cast to [string] failed for value "["chicken"]" (type string) at path "friends.0"
    at SchemaArray.cast (/Users/l/Desktop/node_modules/mongoose/lib/schema/array.js:392:15)
    at convertTo_id (/Users/l/Desktop/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:515:28)
    at getModelsMapForPopulate (/Users/l/Desktop/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:242:13)
    at populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4399:21)
    at _populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4370:5)
    at /Users/l/Desktop/node_modules/mongoose/lib/model.js:4347:5
    at promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
    at Mongoose._promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/index.js:1149:10)
    at Function.Model.populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4345:23)
    at model.populate (/Users/l/Desktop/node_modules/mongoose/lib/document.js:3940:19)
    at /Users/l/Desktop/node_modules/mongoose/lib/document.js:4018:10
    at /Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at model.Document.execPopulate (/Users/l/Desktop/node_modules/mongoose/lib/document.js:4017:10)
    at run (/Users/l/Desktop/repro.js:31:35) {
  stringValue: '"["chicken"]"',
  messageFormat: undefined,
  kind: '[string]',
  value: '["chicken"]',
  path: 'friends.0',
  reason: CastError: Cast to string failed for value "["chicken"]" (type CoreMongooseArray) at path "friends"
      at SchemaString.cast (/Users/l/Desktop/node_modules/mongoose/lib/schema/string.js:603:11)
      at SchemaString.SchemaType.applySetters (/Users/l/Desktop/node_modules/mongoose/lib/schematype.js:1106:12)
      at SchemaArray.cast (/Users/l/Desktop/node_modules/mongoose/lib/schema/array.js:388:29)
      at convertTo_id (/Users/l/Desktop/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:515:28)
      at getModelsMapForPopulate (/Users/l/Desktop/node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:242:13)
      at populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4399:21)
      at _populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4370:5)
      at /Users/l/Desktop/node_modules/mongoose/lib/model.js:4347:5
      at promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
      at Mongoose._promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/index.js:1149:10)
      at Function.Model.populate (/Users/l/Desktop/node_modules/mongoose/lib/model.js:4345:23)
      at model.populate (/Users/l/Desktop/node_modules/mongoose/lib/document.js:3940:19)
      at /Users/l/Desktop/node_modules/mongoose/lib/document.js:4018:10
      at /Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
      at new Promise (<anonymous>)
      at promiseOrCallback (/Users/l/Desktop/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10) {
    stringValue: '"["chicken"]"',
    messageFormat: undefined,
    kind: 'string',

[ truncated ]

If the current behavior is a bug, please provide the steps to reproduce.

Repro script:

const mongoose = require('mongoose');

const Cat = mongoose.model('Cat', {
  _id: { 
    type: String,
  },

  name: String,

  friends: [ {
    immutable: true,
    ref: 'Cat',
    type: String
  } ]
});

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });
  
  await mongoose.connection.dropDatabase();

  const friend = new Cat({ _id: 'chicken', name: 'Chicken' });
  await friend.save();

  const myCat = new Cat({ _id: 'waffle', name: 'Waffle', friends: [ friend.id ] });
  await myCat.save();

  await myCat.populate('friends').execPopulate();
}

run().then(() => process.exit(0));

What is the expected behavior?

This should run without an error, and the first entry in friends should be populated.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node 15.14.0
Mongoose 5.12.10
MongoDB 4.4.6

@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label May 19, 2021
@vkarpov15 vkarpov15 added this to the 5.12.11 milestone May 19, 2021
@IslandRhythms
Copy link
Collaborator

I'm not getting an identical error to yours but I am getting a similar error.

(node:2552) UnhandledPromiseRejectionWarning: CastError: Cast to [string] failed for value "["chicken"]" (type string) at path "friends.0"
    at SchemaArray.cast (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\schema\array.js:392:15)
    at convertTo_id (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\helpers\populate\getModelsMapForPopulate.js:515:28)
    at getModelsMapForPopulate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\helpers\populate\getModelsMapForPopulate.js:242:13)
    at populate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\model.js:4399:21)
    at _populate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\model.js:4370:5)
    at C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\model.js:4347:5
    at promiseOrCallback (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\helpers\promiseOrCallback.js:9:12)
    at Mongoose._promiseOrCallback (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\index.js:1149:10)
    at Function.Model.populate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\model.js:4345:23)
    at model.populate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\document.js:3940:19)
    at C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\document.js:4018:10
    at C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
    at model.Document.execPopulate (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\node_modules\mongoose\lib\document.js:4017:10)
    at run (C:\Users\Daniel Diaz\Desktop\Work\debugging\JavaScript\10264.js:31:35)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2552) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:2552) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 21, 2021
vkarpov15 added a commit that referenced this issue May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants