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

schema._clone is not a function in connection.js in V8 #14082

Closed
2 tasks done
aquocbao opened this issue Nov 15, 2023 · 3 comments
Closed
2 tasks done

schema._clone is not a function in connection.js in V8 #14082

aquocbao opened this issue Nov 15, 2023 · 3 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@aquocbao
Copy link

Prerequisites

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

Mongoose version

8.0.0

Node.js version

18.16.0

MongoDB server version

6.2.0

Typescript version (if applicable)

No response

Description

I have an internal project that includes schema created from Mongoose v5.11.17
In the main project, I upgraded Mongoose lib from v5.13.21 to v8.0.0
When try to run the code below, I got the error
dbConnection.model('StoredError', StoredErrorSchema); (StoredErrorSchema is form internal project with v5.11.7)

Line error: node_modules\mongoose\lib\connection.js line 1075

dbConnection.model('StoredError', StoredErrorSchema);get

Steps to Reproduce

  1. Create a schema with Mongoose v5.11.17 in another project
  2. Install that project as a lib in the main project
  3. Run dbConnection.model('StoredError', StoredErrorSchema);
  4. Got error: ERROR CREATING LOGGER: schema._clone is not a function

Expected Behavior

Can work well

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Nov 15, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Nov 15, 2023

Please provide a repro script or environment to demonstrate your issue.

const mongoose = require('mongoose');
const errorSchema = require('../test/14082Import');

const Test = mongoose.model('Test', errorSchema);


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

  await Test.create({
    name: 'Test Testerson'
  });
  console.log(await Test.findOne());
  console.log('done');
}

run();

@aquocbao
Copy link
Author

aquocbao commented Nov 16, 2023

Please help me to check it

const mongoose = require('mongoose');
const { StoredErrorSchema } = require('../share-folder'); (using mongoose v5.13.21 )


const mongoose = require('mongoose');
const { StoredErrorSchema } = require('../share-folder');


async function run() {
  const mongoConnectionString = 'mongodb://127.0.0.1:27017/testing';
  const storeErrorDB = await mongoose.createConnection(mongoConnectionString);
  const Test = await storeErrorDB.model('StoredError', StoredErrorSchema);

  await Test.create({
    name: 'msg',
    env: 'test',
    component: 'a',
  });
  console.log(await Test.findOne());
  console.log('done');
}

run();

Please note that: I can run successfully if I don't use mongoose.createConnection by using your code

But when I try to do this one
const Test = await operationsDB.model('StoredError', new mongoose.Schema({ name: String }, { collection: 'StoredErrorSchema' }));
==> It worked, maybe it's the same Mongoose version in the main project (v8)

@aquocbao aquocbao changed the title schema._clone is not a function in connection.js schema._clone is not a function in connection.js in V8 Nov 16, 2023
@vkarpov15
Copy link
Collaborator

I presume that StoredErrorSchema is an instance of the Schema class from Mongoose 5.11? If that's the case, this error is expected, you shouldn't use schemas from one version of Mongoose as input to models in another version of Mongoose.

For the shared library pattern, we recommend defining Mongoose in peerDependencies in the shared library, rather than in dependencies.

The other alternative is to export StoredErrorSchema as a POJO rather than as a schema instance:

// Replace this:
module.exports = new Schema({
  message: String
});

// With this:
module.exports = {
  message: String
};

And do new Schema(StoredErrorSchem) in the main project.

We should add some notes on shared library patterns in our docs.

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Nov 21, 2023
@vkarpov15 vkarpov15 added this to the 7.6.7 milestone Nov 21, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.7, 7.6.8 Dec 6, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.6.8, 8.0.4 Dec 31, 2023
vkarpov15 added a commit that referenced this issue Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants