arashsheyda / nuxt-mongoose

A Nuxt module for simplifying the use of Mongoose in your project.
https://docs.arashsheyda.me/nuxt-mongoose
69 stars 11 forks source link

Populated-only schemas are not registered #51

Open kaller01 opened 3 months ago

kaller01 commented 3 months ago

I have two models, Photo which holds a reference to Location.

With the following endpoint

import Photo from "~/server/models/Photo"

export default defineEventHandler(async (event) => {
    try {
        return await Photo.find().populate("location");
    }
    catch (error) {
        return error
    }
})

this error is produced:

Schema hasn't been registered for model "locations". Use mongoose.model(name, schema)

I the Location model is not directly used yet (as I am just starting the project) so I assume that the nuxt-mongoose overlooks to register it, even though it is in fact used.

The solution to get this to work for me is to just use the Location model, so the auto registering happens, but I don't think this behavior is intended.

Other then that, I am happy to find this nuxt module exists, great work! :)

arashsheyda commented 3 months ago

@kaller01 thanks for flagging this! It's definitely a hiccup we need to sort out. (I had this issue as well and made it work as you did)

I havn't really looked at the code for this issue but if you find a solution, you're more than welcome to contribute. thanks!

andrija-menuapp commented 3 months ago

Hi, This is not related to this module I think. I've spend some time debugging and I've solve this issue by importing model into the place when I am referencing it in another schema. https://codingmanatee.wordpress.com/2023/07/11/mongoose-missingschemaerror-schema-hasnt-been-registered-for-model-gas/