M
M
MrTartios2021-12-24 21:34:19
MongoDB
MrTartios, 2021-12-24 21:34:19

The server on Node.js gives an error 500 on POST request but writes information to the database, how to fix it?

Todo list on js+node.js with mongodb. The request for cards from the server works correctly, when you try to post a new card, a request is sent to the server, 500 arrives in the browser console referring to this line in the Api , while the card is written to the database correctly, but the request code in the Api is returned with the undefined field
return fetch(`${this.url}/`, {

postToDo(data) {
        return fetch(`${this.url}/`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
        },
            body: JSON.stringify({
                toDo: data,
                creator: 'user'
            })
        })
        .then(res => {return res.json()})
        .catch(err => console.log(err));
    }

code in front
createCard.addEventListener('click', () => {
    const card = new Card({text: inputValue.value, cardSelector: userCard});
    api.postToDo(inputValue.value)
    .then(data => {
        card.createCard();
    });
});

controller
module.exports.createToDo = (req, res) => {
    const data = req.body;
    toDoModel.create({toDo: data.toDo, creator: data.creator})
        .then((res) => {
            res.send(res)
    })
        .catch(err => res.status(500).send(err));
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question