1
exports.create = (req, res) => {
  if (!req.body.task) {
    return res.status(400).send({
      message: "Task Can't be empty",
    });
  }
  const task = new Task({
    task: req.body.task,
  });
  task.save()
    .then((data) => {
      res.send(data);
    })
    .catch((err) => {
      res.status(500).send({
        message: err.message || 'Some error occurred while creating the Task.',
      });
    });
};

This is my function and I tried different ways of putting return but O still get the error:

Expected to return a value at the end of arrow function consistent-return on 1:29.

Can anyone help me fix this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Sarvesh Jhanwar
  • 97
  • 1
  • 2
  • 10
  • I don't know what your problem, but maybe you can try add return before res.send & res.status. Look at the third line, in there, you add return res.status(400). Maybe you should to do to another res. – Titus Sutio Fanpula Jan 20 '20 at 10:54

2 Answers2

1

Add return to your task.save()'s then and catch arrow functions too like this:

task.save().then((data) => {
  return res.send(data);
})
.catch((err) => {
  return res.status(500).send({
    message: err.message || 'Some error occurred while creating the Task.',
  });
});
AndrewL64
  • 15,794
  • 8
  • 47
  • 79
0

I don't think your create function needs to return a specific value, so make sure that the only return you have in there, is without value.

Change:

return res.status(400).send({
  message: "Task Can't be empty",
});

to:

res.status(400).send({
  message: "Task Can't be empty",
});
return;
trincot
  • 317,000
  • 35
  • 244
  • 286