javascript 类型错误:POST 后无法读取未定义的属性“body”(平均值)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29067778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:02:38  来源:igfitidea点击:

TypeError: Cannot read property 'body' of undefined after POST (MEAN)

javascriptnode.jsexpress

提问by Tomasz Ga?kowski

I am trying to send POST data from AngularJS to Express. What I am doing at the moment is trying to send some POST stuff via curl.

我正在尝试将 POST 数据从 AngularJS 发送到 Express。我目前正在做的是尝试通过 curl 发送一些 POST 内容。

I already tried sending the data. It gets send, I get 200 as a response. But whenever I try to access the data via body-parser through req.body I will get a 500 and the error that I posted at the very bottom.

我已经尝试发送数据。它被发送,我得到 200 作为响应。但是每当我尝试通过 req.body 通过 body-parser 访问数据时,我都会得到 500 和我在最底部发布的错误。

I did a lot of research on that matter (past 4-5 hours...) and I think I checked the following common issues:

我对此进行了大量研究(过去 4-5 小时......),我想我检查了以下常见问题:

  • I use some sort of request parser: body-parser
  • I put my app.use(...) with body-parser middleware before requiring for routes
  • I explicitly tell what Content-Type the POST uses
  • I tried both application/json and application/x-www-form-urlencoded
  • 我使用某种请求解析器:body-parser
  • 在需要路由之前,我将我的 app.use(...) 与 body-parser 中间件放在一起
  • 我明确说明 POST 使用什么 Content-Type
  • 我尝试了 application/json 和 application/x-www-form-urlencoded

I surrender. StackOverflowers. Help me, please.

我投降。StackOverflowers。请帮帮我。

curl:

卷曲:

curl -X POST -H "Content-Type: application/json" -d '{"username": "gala", "email": "[email protected]", "password": "123"}' localhost:3000/users

express.js

express.js

// glowny plik przygotowujacy aplikacje express

// wczytanie zaleznosci
var express = require('express')
var stylus = require('stylus')
var parser = require('body-parser')
var morgan = require('morgan')
var compress = require('compression')
var methodOverride = require('method-override')

// przygotowanie aplikacji, obsluga middleware i na koncu zwrocenie obiektu aplikacji
module.exports = function() {
    var app = express()

    if (process.env.NODE_ENV === 'development') {
        app.use(morgan('dev'))
    } else if (process.env.NODE_ENV === 'production') {
        app.use(compress())
    }

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

    app.use(methodOverride())

    // uzycie jade jako silnika szablonow dla html
    app.set('views', __dirroot + '/app/views')
    app.set('view engine', 'jade')

    require(__dirroot + '/app/routes/index.server.route.js')(app)
    require(__dirroot + '/app/routes/users.server.route.js')(app)

    // mozliwosci uzywania statycznych plikow z folderu /public
    app.use(express.static(__dirroot + '/public'))

    return app
}

users.server.route.js:

users.server.route.js:

var users = require(__dirroot + '/app/controllers/users.server.controller.js')

module.exports = function(app) {
    app.route('/users').post(function() {
        users.create()
    })
}

users.server.controller.js:

users.server.controller.js:

var User = require('mongoose').model('User')

exports.create = function(req, res, next) {
    console.log(req.body)
}

Error message:

错误信息:

TypeError: Cannot read property 'body' of undefined
   at Object.exports.create (/home/gala/Projekty/cantr-crafting/app/controllers/users.server.controller.js:4:20)
   at /home/gala/Projekty/cantr-crafting/app/routes/users.server.route.js:5:15
   at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
   at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:110:13)
   at Route.dispatch (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:91:3)
   at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
   at /home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:267:22
   at Function.proto.process_params (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:321:12)
   at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:261:10)
   at methodOverride (/home/gala/Projekty/cantr-crafting/node_modules/method-override/index.js:77:5) 

回答by Phil

I don't use Node or Express much but shouldn't you at least pass reqinto users.create()from the route handler, ie

我不使用节点或快很多,但你不应该至少通requsers.create()从路由处理,即

app.route('/users').post(function(req, res) {
    users.create(req, res)
})

or maybe even

或者甚至

app.route('/users').post(users.create);

回答by Laxminarayana

TypeError: Cannot read property 'body' of undefined after POST (MEAN)you will get this error when your localhost ip is changed or the requesting ip is not correct as you mentioned in the post url. I got the same error when I troubleshoot my ethernet. Then my Ip address was changed. So check your ip. It will work..

类型错误:POST 后无法读取未定义的属性“body”(MEAN),当您的本地主机 ip 更改或请求的 ip 不正确时,您将收到此错误,如您在帖子 url 中提到的那样。我在对以太网进行故障排除时遇到了同样的错误。然后我的IP地址被改变了。所以检查你的ip。它会工作..

回答by Greko2015 GuFn

This happen for me because I have added some testing script on the chrome console and it seems like there were interference. you clean your console manually or run your app in another instance f chrome.

这发生在我身上,因为我在 chrome 控制台上添加了一些测试脚本,似乎存在干扰。您手动清理控制台或在另一个实例 f chrome 中运行您的应用程序。