简体   繁体   中英

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,

        };

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM