T
T
tropicalfruit2018-05-23 11:11:00
React
tropicalfruit, 2018-05-23 11:11:00

How to send a post request with express?

I ran into a problem, tell me what I'm doing wrong?
Most likely I do not understand how the controller works on the express, how can I fix this?
There is a server on express where the following controller is written:

exports.create = function (req, res) {
    if (!req.body.text) {
        res.status(422).send("'text' field must be present in json");
    } else {
        const written = db.get('todos')
            .push({
                id: uuidv1(),
                text: req.body.text,
                completed: false,
                createdDate: new Date().getTime()
            })
            .last()
            .write();
        res.send(written);
    }
};

When trying to send a request
export function createTask(value) {
  return fetch(`todos/`, {
    method: "POST",
    mode: "no-cors",
    headers: {
      "Content-Type": "application/json",
      Accept: "application/json"
    },
    body: JSON.stringify({ text: value })
  }).then(res => res.json());
}

Error 422 appears, which is written in the controller in case req.body.text is missing. I don't understand why it's not sent.

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