nuxt-mongoose
nuxt-mongoose copied to clipboard
defineMongooseModel Schema is not defined when running in production (nuxt start)
hello, I'm trying to deploy my application in production. and I encountered a problem when executing API requests once the application is deployed which returns me error 500 TodoSchema is not defined. here are the steps I use for prod deployment
npm ci
npm run build
node .output/server/index.mjs # or nuxt start
the problem does not appear during the development (nuxt dev) I attach the configuration of my application below would there be a configuration step necessary to include the server/models folder in nuxt ??
"dependencies": {
"nuxt": "^3.11.2",
"nuxt-mongoose": "^1.0.5",
"vue": "^3.4.21",
"vue-router": "^4.3.0"
},
nuxt.config.ts
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
$development: {
modules: ["@nuxtjs/eslint-module"],
},
modules: ["nuxt-mongoose"],
mongoose: {
uri: process.env.CONN_STR,
},
});
server/models/todo.schema.ts
import { defineMongooseModel } from "#nuxt/mongoose";
export const TotdSchema = defineMongooseModel({
name: "Todo",
schema: {...},
});
server/api/todos/[_id].get.ts
export default defineEventHandler(async (event) => {
try {
return await TodoSchema.findOne({ _id: event.context.params?._id });
} catch (error) {
return error;
}
});
Hi @gamegine, no this is find & should work already! what kind of server do you have?
hello, i run on docker node:20.11.1-alpine image i have try to run dev inside the container is working but not the nuxt build/start
nuxt start
╭──PreviewMode─────────────────────────────────────╮ │ │ │ You are running Nuxt production build in preview mode. │ │ For production deployments, please directly use node ./server/index.mjs command. │ │ │ │ Node.js: v20.13.0 │ │ Nitro Preset: node-server │ │ Working directory: .output │ │ │ ╰─────────────────────────────────────────────╯
hello, I found the cause of the problem. its 'uri: process.env.CONN_STR' , in defineNuxtConfig something I didn't know yet about env variables in nuxt during the build nuxt incorporates static values of the variable of env and not the env.xxx when I launch my app the value was not set from env but set on val of build state and therefore set to undefined//null I looked at how the module was loaded, and I saw that if the uri was not set the setup was interrupt
node_modules/nuxt-mongoose/dist/module.mjs
async setup(options, nuxt) {
if (nuxt.options.dev) {
...
}
if (!options.uri) {
logger.warn("Missing MongoDB URI. You can set it in your `nuxt.config` or in your `.env` as `MONGODB_URI`");
return;
}
/// setup module
...
being directly checked from options this makes it impossible to use runtimeConfig{mongoose:{uri}}
hey @gamegine, sorry for the late response
we changed the behaviour some time ago so if uri
is not provided to proceed, just published a new version and that should work.
hello, I updated, and that solved the problem :) I also changed my nuxt config to load the config from the env variables in runtime and it also works.
export default defineNuxtConfig({
///
modules: ["nuxt-mongoose"],
runtimeConfig: {
mongoose: { uri: process.env.NUXT_MONGOOSE_URI },
},
});
it might be interesting to add this point to the documentation and change the default config env variable from MONGODB_URI to NUXT_MONGOOSE_URI as specified in https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables. (subject of pull request #44 )