Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

After inserting data on mongodb it shows me Cannot read property '_id' of undefined in node js ?

After inserting data on mongodb it shows me Cannot read property '_id' of undefined in node js ?

you can use save method instead of insertOne first instantiate your object by adding new databaseCollection(newService). and after that call save method to insert your collection

Use res.json instead of res.send to enforce Content-Type to be of JSON type

Your code should look like this:

 app.post("/service", async (res, req) => {
    const newService = req.body;
    try {
        const service= await new databaseCollection(newService);
        const savedService=await service.save()
        res.status(200).json(savedService);
    } catch (err) {
        res.status(500).json(err);
    }
});

6 0

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online