Wednesday, 15 March 2017

unable to store values in mongoDb?

my api log

 OPTIONS /api/signup 204 14.010 ms - -
req.body { '{"name":"rahul jain","mobile":"343453","email":"inayath@cantern.in","password":"123","cpassword":"123"}': '' }
POST /api/signup 200 9.296 ms - 56

im making a post request from angularjs to nodejs server and heres my angularjs code:

.controller('RegisterCtrl', function($scope, AuthService, $state, $http) {
  $scope.user = {
    name: '', 
    mobile:'',
    email:'',
    password:'',
    cpassword:''
  };

  $scope.signup = function() {
    console.log("user",$scope.user);
    $http.post("http://localhost:8080/api/signup", $scope.user )
        .then(function (response) {
            return response;
        });
  };
})

and heres my nodejs code:

apiRoutes.post('/signup', function(req, res) { 
  console.log("req",req.body);
  if (!req.body.name || !req.body.password) {
    res.json({success: false, msg: 'Please pass name and password.'});
  } else {
    var newUser = new User({
      name: req.body.name,
      mobile: req.body.mobile,
      email: req.body.email,
      password: req.body.password,
      cpassword: req.body.cpassword
    });
    // save the user
    newUser.save(function(err) {
      if (err) {
        return res.json({success: false, msg: 'Username already exists.'});
      }
      res.json({success: true, msg: 'Successful created new user.'});
    });
  }
});

i think the req.body object having key-value and key is my whole data. is that correct? please help me out, thanks in advance.



via Inayath

No comments:

Post a Comment