Nuxt.js: Mongoose-intl plugin validation fails after HMR

Created on 4 Sep 2020  路  5Comments  路  Source: nuxt/nuxt.js

Versions

  • nuxt: v2.14.4
  • node: v14.0.0
  • mongoose: v5.10.3

Reproduction

Use this project to reproduce the issue. It's created from nuxt express-template with mongoose and mongoose-intl package.

Steps to reproduce

  1. npm install
  2. npm run dev
  3. change anything in api/models/blog.js file and save

What is Expected?

Hot reload without any errors.

What is actually happening?

ServerMiddleware Error: Mongoose-intl plugin can be used with String type only.

The issue is pretty strange and I cannot understand why is it happening. In the mongoose-intl plugin, there is a check, which validates that certain field in schema is type of String, and after the HMR it fails. I tried to debug it, and the objects still look valid during the reload.

I think, it started happening after some cache refresh for mongoose was added with https://github.com/nuxt/nuxt.js/pull/7938. If I downgrade to 2.13.3, it works "fine" (showing "model cannot be compiled twice" error).

Before that, we've had a model compilation error (cannot be compiled twice) and I was using a small trick in my other app:

if (process.env.NODE_ENV !== 'production') {
  // Remove cache during HMR
  mongoose.deleteModel(/.+/)
}

Thanks for any help.

bug-report stale

Most helpful comment

"changing a model without re-initing mongoose is impossible" <-- it is possible using deleteModel(). However, based on my understanding of how Nuxt's reloading works, I think that would be cumbersome for users that export models rather than schemas, because every time they change their schema Nuxt reloads the file and recreates the model.

The workaround is to do:

mongoose.deleteModel(/^MyModelName$/);

mongoose.model('MyModelName', schema);

to make sure you delete the model before recreating it.

I opened an issue to add an option to make that more streamlined in this issue: https://github.com/Automattic/mongoose/issues/9406 . I agree that making automated reloading easy is a priority, and we'll consider removing the OverwriteModelError check in a future release.

All 5 comments

If I add mongoose-intl plugin to the isHMRCompatible, I don't see this issue. Maybe Nuxt can expose some configuration for such modules?

function isHMRCompatible (id) {
  return !/[/\\]mongoose[/\\/]/.test(id) || !/[/\\]mongoose-intl[/\\/]/.test(id)
}

Hi, Mongoose maintainer here. What can we do to help to get #7938 reverted? That change unfortunately means HMR doesn't support many Mongoose plugins, and it seems unnecessary.

hey @vkarpov15,
Mongoose has a strict rule to prevent calling mongoose.model(modelName, modelSchema) with the same modelName and different schema. On the production it completely makes sense, but in development, changing a model without re-initing mongoose is impossible.

We did create an exception for Mongoose in #7938 to ensure it is re-init on file change but as you said in #9391 there are many plugins out there that needs reinitialization.

I really don't know internal structure of Mongoose and the reasons of this strict rule. Is it possible to provide config to allow override a model without any side effects? Or simply ignore this check on the development environment.

"changing a model without re-initing mongoose is impossible" <-- it is possible using deleteModel(). However, based on my understanding of how Nuxt's reloading works, I think that would be cumbersome for users that export models rather than schemas, because every time they change their schema Nuxt reloads the file and recreates the model.

The workaround is to do:

mongoose.deleteModel(/^MyModelName$/);

mongoose.model('MyModelName', schema);

to make sure you delete the model before recreating it.

I opened an issue to add an option to make that more streamlined in this issue: https://github.com/Automattic/mongoose/issues/9406 . I agree that making automated reloading easy is a priority, and we'll consider removing the OverwriteModelError check in a future release.

Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
  2. Comment the steps to reproduce it

Issues that are labeled as pending will not be automatically marked as stale.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VincentLoy picture VincentLoy  路  3Comments

bimohxh picture bimohxh  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

mikekidder picture mikekidder  路  3Comments

jaredreich picture jaredreich  路  3Comments