import db from "~~/plugins/mongoose";
export default defineEventHandler(async (event) => {
let b = [];
db.once('open', function () {
const kittySchema = new mongoose.Schema({
name: String
});
// Create a Mongoose model from the schema
const Kitten = mongoose.model('Kitten', kittySchema);
let c = Kitten.find((error, kittens) => {
if (error) {
console.log(error);
} else {
console.log('Kittens:');
kittens.forEach((kitten) => {
b.push(kitten)
});
}
});
console.log(c)
});
return b
})```
#how to return the data as response from mongoDb?
47 messages · Page 1 of 1 (latest)
Hi, what is your exact question here?
Just seeing your code I see you would have to promisify your code with
export default defineEventHandler(async (event) => {
return new Promise((resolve, reject) => {
let b = [];
db.once('open', function () {
const kittySchema = new mongoose.Schema({
name: String
});
// Create a Mongoose model from the schema
const Kitten = mongoose.model('Kitten', kittySchema);
let c = Kitten.find((error, kittens) => {
if (error) {
console.log(error);
} else {
console.log('Kittens:');
kittens.forEach((kitten) => {
b.push(kitten)
});
}
});
console.log(c)
});
resolve(b)
})
})
Though I am sure that there are mongodb libraries that do that for you.
Hi , sorry If was not clear , I just want to get the data from the mongoDB with find and return it as response
I tried your code and i think I asked the same question to chatGPT , it suggested the same as yours ,any other solutions 😦 ?
Mine doesn’t work?
Can you debug with logs that the data actually comes in?
Inside the .find
Check out https://www.prisma.io/ for a better developer experience on querying MongoDB.
I dont see any logs 😦
Kitten.find((error, kittens) => {
if (error) {
console.log('FUCK YOU');
} else {
console.log('Kittens:');
kittens.forEach((kitten) => {
b.push(kitten);
console.log("IS THIS WORKING ?")
});
}
});
});``` here is what I tried
I guess I will look up prisma 🙂
Okay if you don't see anything maybe it's not the right way to use mongoose
Yeah prisma is generally more modern
Thank you for letting me know , I never knew this was a thing
also do I really need to do this ?
is there a simpler way for global store in nuxt ?
In nuxt3 you probably want to use Pinia, vuex is outdated
Check out https://nuxt.com/modules to see what best to use for nuxt, for different purposes like state management, images, etc
Nuxt has also built in state management with useState, so for simple-Medium tasks you don’t even need Pinia
is using state inside composable good enough ?
for example I have a gallery of images , which i have stored in an array and each image will lead to a new subpage like /gallery/2 , I want to pass the image url in the route params but thats scuffed , so I had to use same array in 2 pages
useState is global, you can create a custom composable which holds the state, which you can import anywhere 👍🏻
I suggest to read the get started section in the nuxt docs: https://nuxt.com/docs/getting-started/state-management, especially the shared state section
Wow, this is really cool :DDD
export const useCounter = () => useState('photos', () => {
urls:[lots of image urls],
removeUrl:function(index){
}
} )``` is this possible ?
also I am getting erors when I am trying to use this as object :/
export const useCounter = () => {
const photos = useState('photos', () => [])
const removeUrl = (index) => {
photos.value //...
}
return { photos, removeUrl }
}
try this+
is it not possible to just have 2 properties?
No, usestate itsself only holds the state value
everything else is built around it but still inside the composable
export const useNumbers= ()=>useState('numbers',function(){
return {
number:1
}
//somewhere else
const {number}=useNumbers();
console.log(number)
})``` are objects not returnable ?
I even tried just to log const number=useNumber()
useState will return a ref
You cannot destructure a ref
export const useNumbers = ()=> useState('numbers', () => {
return 1
})
// somewhere else
const number = useNumbers();
console.log(number.value)
That's how it will work
It's just a vue 3 thing
I will look more into it but I am using pinia now and I am having issues
i followed this link
Maybe create a new thread for your pinia issues
sure