#how to return the data as response from mongoDb?

47 messages · Page 1 of 1 (latest)

polar holly
#
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

})```
fiery spear
#

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.

polar holly
#

I tried your code and i think I asked the same question to chatGPT , it suggested the same as yours ,any other solutions 😦 ?

fiery spear
#

Mine doesn’t work?

polar holly
#

its like returning the same empty array

#

declared above

fiery spear
#

Can you debug with logs that the data actually comes in?

#

Inside the .find

polar holly
#

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 🙂

fiery spear
#

Okay if you don't see anything maybe it's not the right way to use mongoose

#

Yeah prisma is generally more modern

polar holly
#

also do I really need to do this ?

#

is there a simpler way for global store in nuxt ?

fiery spear
#

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

Discover our list of modules to supercharge your Nuxt project. Created by the Nuxt team and community.

#

Nuxt has also built in state management with useState, so for simple-Medium tasks you don’t even need Pinia

polar holly
#

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

fiery spear
polar holly
#

Wow, this is really cool :DDD

fiery spear
#

😄

#

Nuxt 3 and the currently fast growing ecosystem is really awesome yeah!

polar holly
#
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 :/

fiery spear
#
export const useCounter = () => {
  const photos = useState('photos', () => [])
  const removeUrl = (index) => {
    photos.value //...
  }

  return { photos, removeUrl }
}

try this+

polar holly
fiery spear
#

No, usestate itsself only holds the state value

#

everything else is built around it but still inside the composable

polar holly
#
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()

fiery spear
#

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

polar holly
#

I will look more into it but I am using pinia now and I am having issues

#

i followed this link

fiery spear
#

Maybe create a new thread for your pinia issues

polar holly
#

sure