简体   繁体   English

Node.js request.body为空

[英]Node.js request.body is empty

Trying to post from a register form in a MEAN application, I can use Postman to successfully post using x-www-form-urlencoded, but the request body in the app is empty when I console.log it. 尝试从MEAN应用程序中的注册表单发布时,我可以使用Postman使用x-www-form-urlencoded成功发布,但是当我进行console.log记录时,应用程序中的请求正文为空。 The register form (part): 注册表(部分):

<form class="form-horizontal" ng-submit="register.saveUser()">

            <div class="form-group">
                <label class="col-sm-2 control-label">Name</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" ng-model="user.userData.name">

api.js (part) api.js(部分)

var bodyParser = require('body-parser');    // get body-parser
var User       = require('../models/user');
var jwt        = require('jsonwebtoken');
var config     = require('../../config');


var superSecret = config.secret;

module.exports = function(app, express) {

    var apiRouter = express.Router();

    apiRouter.post('/register', function(req, res) {

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

            console.log(req.body); //EMPTY

I have app.use bodyParser in server.js before the call to api.js 在调用api.js之前,我在server.js中有app.use bodyParser

server.js (part) server.js(部分)

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var apiRoutes = require('./app/routes/api')(app, express);
app.use('/api', apiRoutes);

I'm doing something wrong but can't see what. 我做错了,但看不到。

Added for @mscdex: register.saveUser() refers to userRegisterController saveUser() function: 为@mscdex添加的:register.saveUser()引用userRegisterController saveUser()函数:

.controller('userRegisterController', function(User) {

    var vm = this;

    // function to create a user
    vm.saveUser = function() {
        vm.processing = true;
        vm.message = '';

        // use the register function in the userService
        User.register(vm.userData)
            .success(function(data) {
                vm.processing = false;
                vm.userData = {};
                vm.message = data.message;
            });

    };  

This uses the userService register function: 这使用了userService注册函数:

// register a user
    userFactory.register = function(userData) {
        return $http.post('/api/register/', userData);
    };

This calls api file which I have posted above. 这将调用我上面发布的api文件。

BTW when I use Postman the console.dir(req.headers['content-type']) shows 'application/x-www-form-urlencoded' and is successful. 顺便说一句,当我使用Postman时console.dir(req.headers ['content-type'])显示为'application / x-www-form-urlencoded'并且成功。

I spent quite a few hours on this today and ultimately the problem of 'undefined' being received by the apiRouter function was due to my use of 我今天花了很多时间,最终apiRouter函数收到“未定义”的问题是由于我使用了

<input type="text" class="form-control" ng-model="user.userData.name">

whereas I should have used 而我应该使用

<input type="text" class="form-control" ng-model="register.userData.name">

because I had used 因为我曾经

.when('/register', {
            templateUrl: 'app/views/pages/register.html',
            controller: 'userCreateController',
            controllerAs: 'register'
        });

in the apiRoutes file. 在apiRoutes文件中。 I'm still learning the usage of ControllerAs in this context. 在这种情况下,我仍在学习ControllerAs的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM