简体   繁体   中英

Expected to return a value at the end of arrow function consistent-return

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 i 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 ?

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.',
  });
});

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM