adnanrahic / nodejs-restful-api

How to create a RESTful CRUD API using Nodejs?
327 stars 166 forks source link

Building client side #8

Closed MirzaLeka closed 1 year ago

MirzaLeka commented 6 years ago

Hi. I'm getting a 404 error when posting new users. Is everything OK on the backend? In case you're wondering I did swap your db URI with my own, mongodb://localhost:27017/UsersApp

Here's my JS file

function register() {

    let name = document.querySelector("#exampleInputName").value;
    let email = document.querySelector("#exampleInputEmail").value;
    let password = document.querySelector("#exampleInputPassword").value;

    if (name == "" || email == "" || password == "") {
        return alert("Fill the form to continue");
    }

    let data = {
        "name" : name,
        "email" : email,
        "password" : password
    };

    $.ajax({
        type: "POST",
        url: "/",
        contentType : 'application/json',
        dataType: "json",
        data : JSON.stringify(data),
        success: function(data) {

            console.log(data); 
        },

        error: function(err) {

            console.log(err);

        }
    });

}

I also tried changing post users route, but that didn't work either

    let body = _.pick(req.body, ["name", "email", "password"]); // lodash

    let user = new User(body);

    user.save().then((user) => {
    res.status(200).send(user);

  }).catch((e) => {
  res.status(400).send(e);
  });