Put Request Req. Body Is Empty

Put Request Req. Body Is Empty

As all my requests are working fine, I have a problem with the put. req.body stays empty and then gives that error :

errmsg: "'$set' is empty. You must specify a field like so: {$set: {: ...}}"

PUT :

router.put('/books/:name', (req, res, next) => {
    const localdb = db.client.db(process.env.DB_NAME);
    const collection = localdb.collection(process.env.COLL_BOOKS);
    collection.replaceOne(
        { "name": req.params.name },
        { $set: req.body },
        function (err) {
            if (err) throw err
            res.status(201).send(true);

        });

App.js

const express = require('express'),
    app = express();
os = require('os');
const bodyParser = require('body-parser');
const cors = require('cors');
const router = require('./router.js')
require('dotenv').config()


app.use(cors());
app.use(bodyParser.json());

app.use('/api/v1', router);

const port = (process.env.PORT || '3001');

let server = app.listen(port, os.hostname(), () => {
    let host = server.address().address,
        port = server.address().port;
    console.log("Example app listening at ", host, port);
});

axios request :

 updateItem = newBook => {
        Axios.put(process.env.REACT_APP_API_PATH_BOOKS + `${newBook.name}`, newBook)
            .then(res => {
                this.setState({ newBook: res.data });
                this.props.history.push('/admin');
            })
            .catch(err => console.log(err));
    }

I don't understand what I am doing wrong

1

1 Answer

Make sure you don't have any middlware stripping or incorrectly parsing the body. For instance, you may have a JSON body parser, and not be sending JSON data with JSON application headers.

Can you give a bit of context, in code, for how you are making the put request and also the result of logging the req in a pastebin?

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.