question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Self populating doesn't work in 6.1.8

See original GitHub issue
const historySchema = new mongoose.Schema(
  {
    plan: Object,
    parents: [this],
    isDeleted: Boolean,
    isDraft: Boolean,
  },
  {
    timestamps: true,
  }
)

export default mongoose.model("history", historySchema)

Look at parents. It worked perfectly in ^5.13.14 but not in ^6.1.8. I tried some ways to make it work but unfortunately without any effect.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
AndreyKomincommented, Feb 21, 2022

@vkarpov15 Hi there, sorry for delay. Providing the script. So try to run it under ^5.13.14 and then under ^6.2.2 and then compare output of console.log(result)

const mongoose = require("mongoose")
const MONGO_URI = "mongodb://localhost:27017/test-issue"

const historySchema = new mongoose.Schema({
  data: String,
  parents: [this],
})

const History = mongoose.model("history", historySchema)

const run = async () => {
  await mongoose.connect(MONGO_URI)

  await History.deleteMany({}) // Freshening the collection

  const parentHistoryItem = await History.create({ parents: [], data: "Parent data in here" })
  const parentObjectId = parentHistoryItem._id

  const childHistoryItem = await History.create({ parents: [parentObjectId], data: "Child data" })
  const childObjectId = childHistoryItem._id

  const result = await History.findById(childObjectId).populate("parents")

  console.log(result)
}

;(async function () {
  await run()
})()

0reactions
vkarpov15commented, Mar 19, 2022

@AndreyKomin the script as written worked by accident, and isn’t a feature that Mongoose supports. The problem is that, in your script, Mongoose 5 interpretted [this] to be [], an array of any type. The correct way to define your schema is as follows:

const historySchema = new mongoose.Schema({
  data: String,
  parents: [{ type: mongoose.ObjectId, ref: 'history' }],
})

In Mongoose 6, we also don’t fall back to using the History model when using Query.prototype.populate(). We’ll add a note to our migration guide about this. But, in the meantime, please change how parents is defined in your schema and that should fix this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eNavFit User Guide - MyNavyHR
1.1 Introduction. The eNavFit interface: • Produces formal evaluation data for use in various downstream talent management processes.
Read more >
Release notes for the Microsoft JDBC Driver for SQL Server
This article lists the releases of the Microsoft JDBC Driver for SQL Server. For each release version, the changes are named and described....
Read more >
Hibernate ORM 6.1.6.Final User Guide - Red Hat on GitHub
Working with both Object-Oriented software and Relational Databases can be cumbersome and time-consuming. Development costs are significantly higher due to ...
Read more >
Err Documentation - Errbot
Err Documentation, Release 6.1.8. Errbot is a chatbot, a daemon that connects ... __init__ on StoragePluginBase, Errbot will populate self.
Read more >
Content Security Policy Level 3 - W3C
Publication as a Working Draft does not imply endorsement by W3C and its ... The sandbox directive is used to populate the CSP-derived ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found