0
votes

I am trying to update a data using a specific ID which is not working but gives above error.

When I update, first I search the database for that specific id and then save the data to MongoDB

here is my server.js

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');

//importing MongoDB model schema
let ToDo = require('./todo.model');

const app = express();
const todoRoutes = express.Router();

const PORT = 4000;

//middlewares
app.use(bodyParser.json());
app.use(cors());
app.use('/todos', todoRoutes);

//connection to the MongoDB database
mongoose.connect('mongodb://127.0.0.1:27017/todos', {useNewUrlParser: true});
const connection = mongoose.connection;
connection.once('open', () =>{
    console.log("Connected to the MongoDB through port: 27017");
 });

app.listen(PORT, () => { 
    console.log(`Listening to port: ${PORT}`);
});

//get all data - removed to show only the code snippet I am getting errors

//get data by an ID - removed to show only the code snippet I am getting errors

//add items to database -removed to show only the code snippet I am getting errors

//update items
todoRoutes.route('/update/:id').post((req, res) => {
    let id = req.params.id;
    ToDo.findById(id, (err, todo) => {
        if(err) throw err;
        if(!todo) res.status(400).send("No data found");

        todo.todo_description = req.body.todo_description;
        todo.todo_responsible = req.body.todo_responsible;
        todo.todo_priority = req.body.todo_priority;
        todo.todo_completed = req.body.todo_completed;
        res.end();

        todo.save().then(todo => {
            res.json(200).send("Data Updated! " + todo);
            res.end();
        }).catch(err => {
            res.status(400).send("Error occured! " + err);
        });
    });
});

This is the error I am getting...

Error

Can someone please help me?

2

2 Answers

1
votes

This error usually means that you send a response more than once.

Notice that you send two responses one after the other res.json() and res.end()

If you want for some reason to just end the response, use res.end(), otherwise use res.status(200).json({ result: ‘Data updated’ + todo })

If you send both, it will complain about trying to modify the response (via res.end()) after sending it (via res.status().json())

1
votes

In the '/update/:id' route, you're sending a res.end() then doing it again 3 lines later. If you remove the first res.end(), it should work.

You should also return if todo is missing:

todoRoutes.route('/update/:id').post((req, res) => {
    let id = req.params.id;
    ToDo.findById(id, (err, todo) => {
        if(err) throw err;
        if(!todo) return res.status(400).send("No data found");

        todo.todo_description = req.body.todo_description;
        todo.todo_responsible = req.body.todo_responsible;
        todo.todo_priority = req.body.todo_priority;
        todo.todo_completed = req.body.todo_completed;

        todo.save().then(todo => {
            res.status(200).send("Data Updated! " + todo);
        }).catch(err => {
            res.status(400).send("Error occured! " + err);
        });
    });
});