Closed wangweiji1006 closed 1 year ago
hello @wangweiji1006, the connection is being add to your project by runtime(more detail), you just need to pass your mongodb uri by setting it in your .env
as MONGODB_URI
or set it in your nuxt-config
like:
mongoose: {
uri: 'mongodb://127.0.0.1:27017/nuxt-mongoose',
},
let me now if this solve your problem, thank you
I too have the same problem:
500 The requested module '/_nuxt/node_modules/mongoose/dist/browser.umd.js?v=a12caa11' does not provide an export named 'connection'
This is what I've tried:
MONGODB_URI=mongodb://11.22.33.44:27017/testingdb?retryWrites=true&w=majority
export default defineNuxtConfig({
// ...
modules: ["@nuxtjs/tailwindcss", "@pinia/nuxt", "nuxt-icon","nuxt-mongoose"],
pinia: {
autoImports: [
// automatically imports `defineStore`
'defineStore', // import { defineStore } from 'pinia'
['defineStore', 'definePiniaStore'], // import { defineStore as definePiniaStore } from 'pinia'
],
},
mongoose: {
uri: process.env.MONGODB_URI,
options: {},
},
});
export const User = defineMongooseModel({
name: 'User',
schema: {
email: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
},
})
<template>
<div>{{ user }}</div>
</template>
<script setup lang="ts">
const mongoose = useMongoose()
const user = await mongoose.db.collection('users').findOne()
</script>
Nuxi 3.4.3 14:50:13
Nuxt 3.4.3 with Nitro 2.4.0 14:50:13
14:50:16
> Local: http://localhost:3000/ 14:50:12
> Network: http://11.22.44.55:3000/
ℹ Using Tailwind CSS from ~/assets/css/tailwind.css nuxt:tailwindcss 14:50:16
✔ nuxt-mongoose is ready! 14:50:18
ℹ Tailwind Viewer: http://localhost:3000/_tailwind/ nuxt:tailwindcss 14:50:21
ℹ Vite client warmed up in 8123ms 14:50:32
✔ Nitro built in 2634 ms nitro 14:50:34
ℹ Connected to database
When I refresh http://localhost:3000/testing I can briefly see the test user record on the page:
{ "_id": "69eae420cc187c0411fff69e", "name": "testinguser", "email": "user@testing.com" }
...but then a second later the page is replaced with the error 500.
So it's technically working but something is causing the 500. I've tested in chrome (has a lot of browser plugins) and firefox (no browser extensions).
Any ideas on what could be causing it?
Thanks.
@preginald thanks, I think we should remove useMongoose
from the package, tried to make it work but it is so buggy :)
I believe the best practice here would be to first create model
as you created(you can move your models
folder to server/utils
so it autoimport them), then create api
endpoints for example server/api/users/index.get.ts
:
export default defineEventHandler(async () => {
return await User.find()
})
and then in your view you can use it like:
<script setup lang="ts">
const { data: users } = useFetch('/api/users')
</script>
Thanks for responding @arashsheyda,
No probs. I'll remove 'nuxt-mongoose' for now and go for 'mongoose' as I've already have this working on another nuxt app. I'll also try your suggestion of moving the models folder for the auto loading goodness.
Wish you all the best :)
Thank you, for now it's removed(if you found a way to use connection in composables, you can open a PR, I'm more than happy to accept it :D )
thanks, whish you the same
hello @wangweiji1006, the connection is being add to your project by runtime(more detail), you just need to pass your mongodb uri by setting it in your
.env
asMONGODB_URI
or set it in yournuxt-config
like:mongoose: { uri: 'mongodb://127.0.0.1:27017/nuxt-mongoose', },
let me now if this solve your problem, thank you
soory,I have been busy with project development during this period, Previously, I was studying nuxt3 technology , Unable to test this issue, Now it has turned to nuxt2 + expreess + mongoose, However, I found that your module can directly use on the Vue page===> const mongoose=useMongoose() Const user=await mongoose. db. competition ('users'). findOne() is a bit curious..... This approach seems to expose backend security issues....
@wangweiji1006 yes, I had remove the useMongoose
, thank you
How can I add methods to the model defineMogooseModel()
How can I add methods to the model defineMogooseModel()
How can I add methods to the model defineMogooseModel()
@alejito871026 sorry for late answer, you can define your methods in your model(schema) file, an exmaple would be like:
import { defineMongooseModel } from '#nuxt/mongoose'
import mongoose from 'mongoose'
export const UserSchema = defineMongooseModel({
name: 'User',
schema: {
name: {
type: 'string',
},
},
options: {
statics: {
async findByName(slug: string) {
return await this.findOne({ slug })
}
},
methods: {
async test() {
return await mongoose.model('User').findOne()
},
},
},
})
hi, i use
page return error: The requested module '/_nuxt/node_modules/mongoose/dist/browser.umd.js?v=d38441c5' does not provide an export named 'connection'
Where should I define connection ? in composables or plugins ?
import { defineMongooseConnection } from '#nuxt/mongoose' export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')