表示未将对象从服务发送到 API 控制器

Express not sending object from Service to API controller

试图了解用户身份验证以便集成到我的网络应用程序中。我一直在关注 http://www.sitepoint.com/user-authentication-mean-stack/ 作为指南。

我是 webdev 的新手,所以我无法找到合适的东西来解决我的问题。问题是当我尝试注册一个新用户时,我的 api 控制器没有收到我的对象。

register.controller.js

(function () {
  angular
  .module('misplaced')
  .controller('registerCtrl', registerCtrl);

  registerCtrl.$inject = ['$location', 'authentication'];
  function registerCtrl($location, authentication) {
    var vm = this;
    vm.onSubmit = function () {
    authentication
      .register(vm.credentials)
      .error(function(err){
        alert(err);
      })
      .then(function(){
        $location.path('profile');
      });
  };

authentication.service.js

(function () {
 angular
 .module('misplaced')
 .service('authentication', authentication);

 authentication.$inject = ['$http', '$window'];
 function authentication ($http, $window) {
   register = function(user) {
   return $http.post('/api/register', user).success(function(data){
     saveToken(data.token);
   });
 };

authentication.js

module.exports.register = function(req, res) {

  var user = new User();
  user.name = req.body.name;
  user.email = req.body.email;
  user.setPassword(req.body.password);

  user.save(function(err) {
    var token;
    token = user.generateJwt();
    res.status(200);
    res.json({
      "token" : token
    });
  });
};

通过一些 console.log 我已经跟踪到该对象从 register.controller 中创建得很好并且传递给 authentication.service 很好,但是当我尝试 console.log authentication.js 文件中的对象是空的。有人可以帮我解释一下可能会发生什么吗?提前致谢!

这听起来像是 Express 配置问题,您可能没有在 Express 中使用 bodyParser 模块。

var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());

这将允许您的中间件访问 req.body

中的请求正文属性