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.

PR #12254 introduced bug: Methods defined in shared schemas get removed

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.5

Node.js version

16.6

MongoDB server version

6.0.1

Description

PR https://github.com/Automattic/mongoose/issues/12254 introduces a bug where instance methods defined in shared schema get deleted.

Problem located in line 38 in file mongoose/lib/helpers/model/applyMethods.js

Commit https://github.com/Automattic/mongoose/commit/9fbf92c3a8c6f0717beb404f4020832aafe93589#diff-9de3851c9d06234f6bef122196730d35ce2dc68fc2b609824b1676194608e4fb

Steps to Reproduce

  1. Install mongoose 6.5.5
  2. Run this bug demo script:
const mongoose = require('mongoose')
mongoose.set('debug', true)

const sharedSubSchema = new mongoose.Schema({
  name: {
    type: String
  }
})

sharedSubSchema.methods.sharedSubSchemaMethod = function () {
  console.log('sharedSubSchemaMethod called')
}

const mainDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const MainDocument = mongoose.model('MainDocument', mainDocumentSchema)

const secondaryDocumentSchema = new mongoose.Schema({
  subdocuments: {
    type: [sharedSubSchema],
    required: true
  }
})
const SecondaryDocument = mongoose.model('SecondaryDocument', secondaryDocumentSchema)

const mainDoc = new MainDocument({
  subdocuments: [
    {
      name: 'one'
    }
  ]
})

const secondaryDoc = new SecondaryDocument({
  subdocuments: [
    {
      name: 'secondary'
    }
  ]
})

console.log(mainDoc.subdocuments[0].sharedSubSchemaMethod)
console.log(secondaryDoc.subdocuments[0].sharedSubSchemaMethod)

Expected Behavior

Sub-Document method should be defined on both models, not only one.

Buggy output mongoosejs 6.5.5 [Function (anonymous)] undefined

Correct output mongoosejs 6.5.4 [Function (anonymous)] [Function (anonymous)]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
backflipcommented, Sep 13, 2022

Just stumbled upon the same problem. For reference: The corresponding PR is https://github.com/Automattic/mongoose/pull/12391

1reaction
maximilianschmidcommented, Sep 13, 2022

Just to clarify: version 6.6.0 also has this bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confluent Platform Component Changelogs | Confluent ...
Common¶. PR-428 - Upgrade Netty ; Kafka¶. PR-11742 - KAFKA-13636: Fix for the group coordinator issue where the offsets are deleted for unstable...
Read more >
Recommended update for terraform-provider-aws | SUSE Support
An update that has one recommended fix can now be installed. Description: This update for terraform-provider-aws fixes the following issues:.
Read more >
DataStax Enterprise 5.1 release notes | DSE 5.1 Admin guide
Resources can be deleted when authorization is enabled, given the correct permissions. (DSP-20749); Fixed a bug where a decryption block cache occasionally was ......
Read more >
September 2021 Galaxy Release (v 21.09) — Galaxy Project 22.05 ...
It is also possible to configure the default Tool Panel View using the domain name of the server, so “flavors” of Galaxy can...
Read more >
Changelog - Changes since 3.3.9-RC1 - phpBB
Bug. [PHPBB3-17058] - Special character issue in emails from PHP 8.0 and higher ... [PHPBB3-15594] - The deprecated AOL contact field should be...
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