Thursday 4 May 2017

undefined is not an object (evaluating 'storeDataFactory.createUser(user).then

the function runs and console.log shows the user object on the backend. I don't understand why it's telling me there is an issue here, and really need some guidance.

vm.register = function() {
    //check that passwords match
  if(vm.password != vm.passwordRepeat) {
    vm.registerError = "Passwords must match.";
    return;
  } else {
    var username = vm.username;
    // console.log("Valid form.  Checking for existing user",username);
    storeDataFactory.userExists(username).then(function(response){
      //if user exists, return error
      if(response.data.length > 0) {
        vm.registerError = "A user with email " + username + " already exists.  Please login.";
        return;
      } else {
        //if no user exists
        if(response.data.length == 0) {
          // console.log("No user exists.  Continue with registration.");
        }
        //assign info to user object
          var user = {
          username: vm.username,
          password: vm.password,
          name: vm.name,
          phone: vm.phone
        };
        **storeDataFactory.createUser(user).then(function(response){**
          vm.user = response.data;
          console.log("Created user", vm.user);
          if(response.data.length > 0) {
            console.log("Created user", vm.user);
            vm.registerMessage = "Successful registration, please login";
            vm.registerError = null;
            vm.user = response.data;  
          }
        }).catch(function(error){
            console.log(error);
            vm.registerError = "There was an error creating your account.";
            vm.registerMessage = null;
            });  
      }
    });
  }

};

The backend code:

    module.exports.register = function(req, res) {
  console.log('registering user', req.body);
  //create the user object with hashed pass
  User
    .create({
    username: req.body.username,
    name: req.body.name,
    phone: req.body.phone,
    password: bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(10))
  }, function(err, user) {
      if (err) {
        console.log("Error creating account");
        res
          .status(400)
          .json(err);
      } else {
        console.log("Account created!", user);
        res
          .status(201)
          .json(user);
      }
    });
};

Account created! and the user object are logged on the backend. It just won't display that damn Successful Registration! Please login. message.



via user3561890

No comments:

Post a Comment