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
Mongoose & serverless offline "Cannot overwrite model once compiled." #258
Comments
Hello, I run in the same problem. --skipCacheInvalidation -c Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed. |
I'm using a microservice architecture, so my model are in multiple files and services like: They are multiple handlers referencing the same model. After I put the --skipCacheInvalidation -c flag it stopped doing this error when I call 2 times in a row the get.js service, but when I change and call the create.js after the get.js was called I get the same error as before. Anyone? |
you are compiling user schema multiple times, the reason is you call mongoose.model('User', UserSchema) in the first request and then in the second call too. |
I know that, but with the microservice architecture this is expected right? I have a require of my model in each service I use that model. |
what about this? |
Hey guys! I got a nice hack to avoid this problem using the mongoose method to get an already declared if (!modelAlreadyDeclared()) {
const Users = mongoose.model('Users', yourSchema)
}
function modelAreadyDeclared () {
try {
mongoose.model('Users') // it throws an error if the model is still not defined
return true
} catch (e) {
return false
}
} |
manage to resolve this problem via the following snippet, which restrict the behaviour in local offline environment as well as doesn't stop hot reload of models if (process.env.IS_OFFLINE) {
delete mongoose.connection.models.Item;
}
export default mongoose.model('Item', Item); |
It is also possible to circumvent this issue by using the new |
@harunsmrkovic Just tack on I read that it kicks off a separate process for each function, but I'm still getting the |
Hello @adrianovalente, there is a misspelling in your code: so:
|
The solution for me was the following code:
After read a comment from: that is a resume from: but, in deed, it suggest for mocha:
I hope this help ... |
I had this problem. |
--skipCacheInvalidation worked for me, but gave me problems in production. This solved it in my case; module.exports = user` |
can you share some links to integrate nodemon in serverless-http powered project? |
|
@PabloEzequiel solution worked for me |
@gokul-t 's solution worked for me. Is there any issue with using |
|
When I use this solution this gives Issue |
Adding this before schema declaration worked for me !
|
How this comment don't have million like |
Thanks to @adrianovalente and @gokul-t that I could solve this error. Faced the same issue, gone through the discussion here, and liked both the approaches i.e. By @adrianovalente here and By @gokul-t here The first approach gave a great TS autocompletion support in the handlers after adding Model Type which is one of the purposes for TS but it was a lot of extra code for me for each model The second approach was clean and precise but I couldn't get the TS support and error handling in the handler, it just became JS. Hence I used the Interface approach from TS and came up with the code below which is a bit precise and also uses TS as its most. Just sharing as it could be helpful to anyone in the future.
|
@premraval-pr this is really good solution work in my case Thank you very much |
Thank you for sharing. |
@premraval-pr This is exactly what I needed. Thanks! |
thanksss |
Perfect. This is exactly what I needed. |
Hello,
When using mongoose + mongodb + serverless offline, cannot make 2 calls in a row => the second one will not work & will exit with error:
'MongooseError: Cannot overwrite User model once compiled.'
Step to reproduce:
git clone git@github.com:serverless/examples.git
cd aws-node-rest-api-mongodb
npm install
Error:
curl -XPOST -H "Content-type: application/json" -d '{"name":"name","firstname":"fname","birth":"01/01/1990","city":"Paris"}' 'http://localhost:3000/user'
curl -XGET -H "Content-type: application/json" 'http://localhost:3000/user/USER_ID'
Expected result:
Additional information:
Any idea why the provided example doesn't work with offline ?
Thanks
The text was updated successfully, but these errors were encountered: