无法在mongoDb中存储值?

时间:2017-03-15 15:14:25

标签: angularjs node.js mongodb express

我的API日志:

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

我正在发送从angular.jsnode.js服务器的帖子请求,这是我的angular.js代码:

.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;
        });
  };
})

这是我的node.js代码:

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.'});
    });
  }
});

我认为具有键值和键的req.body对象是我的整个数据。那是对的吗?请提前帮助我。

0 个答案:

没有答案