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

DocumentArray#create() throws an error when creating a subdoc with a manually populated path #10749

Closed
vkarpov15 opened this issue Sep 17, 2021 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@vkarpov15
Copy link
Collaborator

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

Bug

What is the current behavior?

When checking SchemaType#_isRef(), fullPath() may be undefined if the subdoc was just create() -ed. This means the below script:

'use strict';
  
const mongoose = require('mongoose');
const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  console.log(mongoose.version);
  await mongoose.connect('mongodb://localhost:27017/test');
  await mongoose.connection.dropDatabase();

  const schema = new Schema({ subdoc: { ref: 'Test2', type: 'ObjectId' }, arr: [{ num: Number }] });
  const Test = mongoose.model('Test', schema);
  const Test2 = mongoose.model('Test2', Schema({ name: String }));

  const doc2 = await Test2.create({ name: 'test' });
  const doc = await Test.create({ subdoc: doc2, arr: [] });

  const subdoc = doc.arr.create({ num: '42' });

  await subdoc.validate();

  doc.arr.push(subdoc);
  await doc.save();
  console.log('Done');
}

Throw an error on the doc.arr.create() call:

TypeError: Cannot read property 'endsWith' of null
    at model.Document.populated (/home/val/Workspace/MongoDB/mongoose/lib/document.js:4165:24)
    at Function.SchemaType._isRef (/home/val/Workspace/MongoDB/mongoose/lib/schematype.js:1388:17)
    at ObjectId.cast (/home/val/Workspace/MongoDB/mongoose/lib/schema/objectid.js:225:18)
    at ObjectId.SchemaType.applySetters (/home/val/Workspace/MongoDB/mongoose/lib/schematype.js:1122:12)
    at ObjectId.SchemaType.getDefault (/home/val/Workspace/MongoDB/mongoose/lib/schematype.js:1069:25)
    at $__applyDefaults (/home/val/Workspace/MongoDB/mongoose/lib/document.js:509:24)
    at EmbeddedDocument.Document (/home/val/Workspace/MongoDB/mongoose/lib/document.js:138:7)
    at EmbeddedDocument.Subdocument (/home/val/Workspace/MongoDB/mongoose/lib/types/subdocument.js:27:12)
    at EmbeddedDocument.ArraySubdocument [as constructor] (/home/val/Workspace/MongoDB/mongoose/lib/types/ArraySubdocument.js:36:15)
    at new EmbeddedDocument (/home/val/Workspace/MongoDB/mongoose/lib/schema/documentarray.js:115:17)

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

What is the expected behavior?

No error

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

Node.js 14.4
Mongoose 6.0.6
MongoDB 4.4.0

@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 17, 2021
@vkarpov15 vkarpov15 added this to the 6.0.7 milestone Sep 17, 2021
vkarpov15 added a commit that referenced this issue Sep 17, 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

1 participant