0

I'm trying to brush up on my JS and working on a TODO app with just vanilla JS.

I'm trying to integrate with an already written express server, which has an HTTP API

Here are the relevant methods from the server:

function isValid(todo) {
  return todo.complete != undefined && todo.text != undefined;
}
function AddTodo(todo) {
  todo.id = uid();
  Todos.push(todo);
}
app.post('/todos', function(req, res) {
  var todo = req.body;
  if (!isValid(todo)) {
    res.status(422).send(JSON.stringify({
      message: 'invalid todo'
    }));
  }
  else {
    var addedTodo = AddTodo(todo);
    res.status(200).send(JSON.stringify(todo));
  }
});

Here is the code I'm trying to use

var newTodo = {
    complete : false,
    text: 'lorem ipsum test todo'
};

//save
$.post("/todos", newTodo, function(data){
     alert(" success");
});

Unfortunately this approach is generating a 422 error and I can't seem to figure out why.

presswanders
  • 655
  • 6
  • 8

0 Answers0