Doubt regarding building an api

Ok thanks,
When I try to send data through postman.


And then I try to find that data inside req object. I’m not getting anything.
When I do console.log(req.body). it returns a empty object
This is my code

router.post('/items', async (req, res) => {
    const {name, image} = req.body;
    const {secret_key} = req.headers;

    // Authanticating the user
    if(secret_key !==  process.env.MY_SECRET_KEY) {
        return res
            .status(403)
            .send({error: 'You are not allowed to add data!😠'});
    }
    console.log(req)

    Checking data values
    if(!name || !image) {
        return res
            .status(422)
            .send({error: 'There is no name and image'});
    }

    try {
        const item = new Item({name, image});
        await item.save();
        res.send(item);
    } catch (error) {
        res.status(422).send({error: error.message});
    }
})

Is your app on GitHub or somewhere I can pull it down?

I wouldn’t mind figuring this out for you after work today and I’ll explain what I did after.

In the meantime you can also try giving this tutorial a read. It walks you through installing a couple libraries which help with handling image uploads: https://attacomsian.com/blog/uploading-files-nodejs-express

I’ll try to implement that. If I will not able to do that then I’ll share the github repo with you.
You are so helpful.
Thank You

1 Like