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

Replacing unpopulated properties with populated properties doesn't work #8285

Closed
tho-masn opened this issue Oct 28, 2019 · 1 comment
Closed
Milestone

Comments

@tho-masn
Copy link

tho-masn commented Oct 28, 2019

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

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

  1. Request a document and populate it's properties
  2. Request the same document without populating the properties
  3. Assign the populated properties from document in step 1 to the document in step 2 -> Document 2 still has the unpopulated version assigned

Looks like the bug got introduced with the fix in #8247

This script reproduces the behaviour (works in 5.7.5 and fails in later version)

const mongoose = require('mongoose');
mongoose.set('debug', true);

const GITHUB_ISSUE = `gh8285`;
const connectionString = `mongodb://@localhost:27017/${ GITHUB_ISSUE }`;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString, { useNewUrlParser: true });
  await mongoose.connection.dropDatabase();

  const referenceSchema = new mongoose.Schema({
    name: {
      type: String
    }
  });
  const Reference = mongoose.model('Reference', referenceSchema);
  const referenceDoc = new Reference({ name: "Reference 1" });
  await referenceDoc.save();

  const subSchema = new mongoose.Schema({
    referenceProp: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Reference',
      required: true
    }
  })

  const parentSchema = new mongoose.Schema({
    child: {
      type: subSchema,
      required: true
    }
  })
  const Parent = mongoose.model('Parent', parentSchema);
  const parentDoc = new Parent({
    child: {
      referenceProp: referenceDoc._id
    }
  });
  await parentDoc.save();

  const parentDocSaved = await Parent
    .findById(parentDoc._id)
    .populate('child.referenceProp')

  const parentDocSaved2 = await Parent
    .findById(parentDoc._id)
  parentDocSaved2.child.referenceProp = parentDocSaved.child.referenceProp

  console.log("\nExpected output: " + parentDocSaved)
  console.log("\nActual output: " + parentDocSaved2)
}

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

The snippet works as expected in 5.7.5 and doesn't work in later versions anymore.

@vkarpov15
Copy link
Collaborator

This looks like a duplicate of #8273, I can confirm your script works with the fix for #8273. Thanks for the detailed repro script and sorry for the trouble, the fix will be in 5.7.8.

@vkarpov15 vkarpov15 added this to the 5.7.8 milestone Nov 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants