lemilevoj
lemilevoj

Reputation: 31

Trying to read data from Mongo

so I already have the data in Mongo, but the problem is the Mongo doesn't read properly the data from the backend. This is my backend code:

app.get ('/posts', async (req , res) => {
let db = await connect();
let posts = req.body;
res.send(posts); 
});

and the code from services:

async get_posts() {
    let response = await Service.get(/posts)
    let doc = response.doc;
        return {
            id: doc._id,
            email: doc.email,
            title: doc.title,
            imageDesc: doc.imageDesc,
            img: doc.img,

        };

}

Upvotes: 2

Views: 45

Answers (1)

You need to use the instance of the db, and an instance to the collection in order to retrieve the posts.

For instance:

app.get ('/posts', async (req , res) => {
    const postsCollection = db.collection("posts");
    const posts = await postsCollection.find();
    return res.json(posts);
});

Read the mongo documentation here

Upvotes: 1

Related Questions