Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4105

Req.body is not iterable in node.js

$
0
0

I am building mock restful API to learn better. I am using MongoDB and node.js, and for testing I use postman.

I have a router that sends update request router.patch. In my DB, I have name (string), price (number) and imageProduct (string - I hold the path of the image).

I can update my name and price objects using raw-format on the postman, but I cannot update it with form-data. As I understand, in raw-form, I update the data using the array format. Is there a way to do it in form-data? The purpose of using form-data, I want to upload a new image because I can update the path of productImage, but I cannot upload a new image public folder. How can I handle it?

Example of updating data in raw form

[ {"propName": "name"}, {"value": "test"}]

router.patch

router.patch('/:productId', checkAuth, (req, res, next) => {const id = req.params.productId;const updateOps = {};for (const ops of req.body) {    updateOps[ops.propName] = ops.value;}Product.updateMany({_id: id}, {$set: updateOps})    .exec()    .then(result => {        res.status(200).json({            message: 'Product Updated',            request: {                type: 'GET',                url: 'http://localhost:3000/products/'+ id            }        });    })    .catch(err => {        console.log(err);        res.status(500).json({            err: err        });    });});

Viewing all articles
Browse latest Browse all 4105


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>