Fetching every document from a single collection in a MongoDB

Viewed 67

So I want to fetch every document in my MongoDB collection. I used method find() to get every property from a document.

    app.get ('/galerija', async (req , res) => {
    let db = await connect();
    let cursor = await db.collection('galerija').find();
    let results = await cursor.toArray();

    res.json(results);
});

What I need is to get all properties in one array (I don't want to use this: data[1].email), so how do I get every property of an email for example? When I try data.email it says that it is undefined. Please help. Here is that file from services:

async dohvati_objavu() {   
    
    let response = await Service.get(`/galerija`);
    
    let data = response.data;
    
    console.log("Current posts in services: "+data.email)  
    
        return data.map( function(data) {return{
            id: data._id,
            email: data.email,
            naslov: data.naslov,
            noviOpisSlike: data.noviOpisSlike,
            slika: data.slikaReference,
        };   
});
},

This is when I test with console.log: enter image description here

In a Vue file I also want to read property of every document in mongo for every single property of objavait also shows me [Object object]:

async dohvatiObjavu(){
         this.objava = await Objave.dohvati_objavu();
        console.log("Current posts: " + this.objava);   
}
0 Answers
Related