Code newbie here!
I'm following this guide to set up a backend server using Node.js, Express and MongoDB - https://www.codementor.io/@olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd
I keep receiving the following error in the terminal when using Postman.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I looked into the fact I may need to add a return statement somewhere, but since I've been following the guide, I shouldn't be having this issue?
Here's my controllers file where I think the problem may be:
'use strict';
var mongoose = require('mongoose'),
Task = mongoose.model('Tasks');
exports.listAllTasks = function (req, res) {
Task.find({}, function (err, task) {
if (err) res.send(err);
res.json(task);
});
};
exports.createATask = function (req, res) {
var newTask = new Task(req.body);
newTask.save(function (err, task) {
if (err);
res.send(err);
res.json(task);
});
};
exports.readATask = function (req, res) {
Task.findById(req.params.taskId, function (err, task) {
if (err) res.send(err);
res.json(task);
});
};
exports.updateATask = function (req, res) {
Task.findOneAndUpdate(
{ _id: req.params.taskId },
req.body,
{ new: true },
function (err, task) {
if (err) res.send(err);
res.json(task);
}
);
};
exports.deleteATask = function (req, res) {
Task.deleteMany(
{
_id: req.params.taskId,
},
function (err, task) {
if (err) res.send(err);
res.json({ message: 'Task successfully deleted' });
}
);
};
Thanks in advance!