N
Nuxt4w ago
FearArt

Import error

i have 2 absolutely identical mongoose model definitions, but one is causing errors and other - don't
import { defineMongooseModel } from "#nuxt/mongoose";

export const User = defineMongooseModel({
name: 'User',
schema: {
DiscordId : {
type: 'string',
required: true,
unique: true,
},
NickName: {
type: 'string',
required: true,
unique: true,
},
StaticId: {
type: 'string',
required: true,
unique: true,
},
Fraction: {
type: 'string',
required: false,
},
Status: {
type: 'string',
required: false,
},
Warns : {
type: Number,
required: false,
},
Rank : {
type: Number,
required: false,
},
}
})
import { defineMongooseModel } from "#nuxt/mongoose";

export const User = defineMongooseModel({
name: 'User',
schema: {
DiscordId : {
type: 'string',
required: true,
unique: true,
},
NickName: {
type: 'string',
required: true,
unique: true,
},
StaticId: {
type: 'string',
required: true,
unique: true,
},
Fraction: {
type: 'string',
required: false,
},
Status: {
type: 'string',
required: false,
},
Warns : {
type: Number,
required: false,
},
Rank : {
type: Number,
required: false,
},
}
})
import { defineMongooseModel } from "#nuxt/mongoose";

export const LogModel = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: Object,
required: true,
},
target: {
type: Object,
required: true,
},
reason: 'string',
argument: 'string',
logType: 'string',
date: Date
}
})
import { defineMongooseModel } from "#nuxt/mongoose";

export const LogModel = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: Object,
required: true,
},
target: {
type: Object,
required: true,
},
reason: 'string',
argument: 'string',
logType: 'string',
date: Date
}
})
It says: https://i.imgur.com/H0wN4Sz.png so basically it's smth with imports
Imgur
7 Replies
kapa.ai
kapa.ai4w ago
Hi @FearArt. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, it appears you're encountering an import error related to the #nuxt/mongoose module. This is a common issue when working with Nuxt and Mongoose integration. Let's address this problem: 1. First, ensure that you have installed the nuxt-mongoose module correctly. You can do this by running:
npm install nuxt-mongoose -D

npm install nuxt-mongoose -D

Stack Overflow Post (https://stackoverflow.com/questions/76986927) 2. Make sure you've properly configured your nuxt.config.ts file to include the nuxt-mongoose module:
export default defineNuxtConfig({
modules: [
'nuxt-mongoose',
],
mongoose: {
uri: process.env.MONGODB_URI,
options: {},
modelsDir: 'models'
}
})

export default defineNuxtConfig({
modules: [
'nuxt-mongoose',
],
mongoose: {
uri: process.env.MONGODB_URI,
options: {},
modelsDir: 'models'
}
})

Stack Overflow Post (https://stackoverflow.com/questions/76986927) 3. The error you're seeing suggests that there might be an issue with resolving the #nuxt/mongoose import. This could be due to a problem with the module resolution or TypeScript configuration. 4. To troubleshoot this, you can try the following: a. Restart your development server and clear any caches.
b. Make sure your tsconfig.json is properly configured. It should extend from the Nuxt-generated TypeScript configuration:
{
"extends": "./.nuxt/tsconfig.json"
}

{
"extends": "./.nuxt/tsconfig.json"
}

Stack Overflow Post (https://stackoverflow.com/questions/78496997) c. If the issue persists, you might need to manually alias the library. Add this to your nuxt.config.ts:
export default defineNuxtConfig({
alias: {
'#nuxt/mongoose': 'nuxt-mongoose/dist/runtime/server/services'
}
})

export default defineNuxtConfig({
alias: {
'#nuxt/mongoose': 'nuxt-mongoose/dist/runtime/server/services'
}
})

Docs > Guide > Concepts > ES Modules > Troubleshooting ESM Issues (https://nuxt.com/docs/guide/concepts/esm#troubleshooting-esm-issues) 5. If none of the above solutions work, it's possible that there might be a version conflict or an issue with the nuxt-mongoose module itself. In this case, you may want to check for any reported issues on the module's GitHub repository or consider updating to the latest version of Nuxt and the module. Remember, when dealing with import issues in Nuxt, it's often helpful to check your project's dependencies, ensure all modules are correctly installed and configured, and verify that your TypeScript setup is correct. If the problem persists after trying these solutions, you may need to create a minimal reproduction of the issue and seek further assistance from the Nuxt community or report it as a potential bug. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@360699173378588674> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
FearArt
FearArtOP4w ago
@kapa.ai Import works fine in first file so should be an error with module
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're right, and I apologize for not catching that detail earlier. Since the import works fine in the first file but not in the second, it's likely not an issue with the module installation or configuration. Let's focus on the differences between the two files: 1. File structure: Make sure both files are in the same directory, typically under the server/models/ folder. The nuxt-mongoose module looks for model definitions in the directory specified by the modelsDir option in your Nuxt config Stack Overflow Post (https://stackoverflow.com/questions/76986927). 2. Naming convention: The first file exports User, while the second exports LogModel. Try renaming LogModel to Log to match the name specified in the defineMongooseModel call:
export const Log = defineMongooseModel({
name: 'Log',
// ... rest of the schema
})

export const Log = defineMongooseModel({
name: 'Log',
// ... rest of the schema
})

3. Schema definition: The User schema uses string literals for types (e.g., type: 'string'), while the Log schema mixes string literals and actual types (e.g., type: Object). Try using consistent type definitions:
export const Log = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: 'object',
required: true,
},
target: {
type: 'object',
required: true,
},
reason: {
type: 'string',
},
argument: {
type: 'string',
},
logType: {
type: 'string',
},
date: {
type: 'date',
}
}
})

export const Log = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: 'object',
required: true,
},
target: {
type: 'object',
required: true,
},
reason: {
type: 'string',
},
argument: {
type: 'string',
},
logType: {
type: 'string',
},
date: {
type: 'date',
}
}
})

4. If the issue persists, try moving the import statement to the top of the file and use a named import:
import { defineMongooseModel } from '#nuxt/mongoose'

import { defineMongooseModel } from '#nuxt/mongoose'

5. Debugging: If none of the above solutions work, you can try debugging the issue. Nuxt provides built-in debugging capabilities. Add the --inspect flag when starting your development server:
nuxi dev --inspect

nuxi dev --inspect

This will allow you to use the Node.js debugger to step through your code and identify where the import is failing Docs > Guide > Going-further > Debugging (https://nuxt.com/docs/guide/going-further/debugging#debugging-with-node-inspector). If you're still encountering issues after trying these suggestions, it might be helpful to create a minimal reproduction of the problem and share it with the Nuxt community for further assistance Docs > Community > Reporting Bugs (https://nuxt.com/docs/community/reporting-bugs). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@360699173378588674> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
FearArt
FearArtOP4w ago
changing to string don't help
import { defineMongooseModel } from "#nuxt/mongoose";

export const LogModel = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: 'string',
required: true,
},
target: {
type: 'string',
required: true,
},
reason: 'string',
argument: 'string',
logType: 'string',
date: Date
}
})
import { defineMongooseModel } from "#nuxt/mongoose";

export const LogModel = defineMongooseModel({
name: 'Log',
schema: {
initiator: {
type: 'string',
required: true,
},
target: {
type: 'string',
required: true,
},
reason: 'string',
argument: 'string',
logType: 'string',
date: Date
}
})
@kapa.ai
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you again. One moment! :books: Sorry @FearArt, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Want results from more Discord servers?
Add your server