Next.js Discord

Discord Forum

NextJS and MongoDB

Answered
West Siberian Laika posted this in #help-forum
Open in Discord
West Siberian LaikaOP
Can you tell me every time I restart the project (npm run dev) and then run the GET function, I get an error

MissingSchemaError: Schema hasn't been registered for model "User".

but after running another Route that uses the User model and then running the above GET function again, there is no error

thanks
Answered by West Siberian Laika
I solved it by importing UserSchema into ProductShema, then changing ref: "User" to ref: UserSchema. Thank you for replying
View full answer

2 Replies

I would say it's because you are populating your product which uses sellerID associated with User Schema however it cant find it since files dont just automatically run. Maybe try including the file which has user schema definition in the file you are calling GET(). Do it before importing product schema file.
West Siberian LaikaOP
I solved it by importing UserSchema into ProductShema, then changing ref: "User" to ref: UserSchema. Thank you for replying
Answer