req.body。variablename是使用body-parser中间件表示的未定义的

时间:2022-12-19 07:43:59

I am using express 4.14.1 version in my Nodejs application. I am also using body parser middleware for parsing form data but when I write console.log(req.body.variablename) I get undefined on the console. my code is as follows

我正在我的Nodejs应用程序中使用express 4.14.1版本。我也在使用主体解析器中间件来解析表单数据,但是当我编写console.log(req.body.variablename)时,我在控制台没有定义。我的代码如下

const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser'); //parses information from POST
const request = require('request');
const mongodb = require('../model/mongodb.js');
const smtpTransport = require('../model/mailer.js');
const Agent = require('../model/agentmodel.js');
const config = require('../config.js');
const intent = require('../intents.js');
const ejs = require('ejs');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// use res.render to load up an ejs view file
router.get('/chat', function(req,res){
    // res.sendFile(path.join(__dirname + '/html/' + '/index.html'));
    res.render('pages/index', { heading: config.property.userheading});         
});

// use res.render to load up an ejs view file
router.get('/', function(req,res){
  // res.sendFile(path.join(__dirname + '/html/' + '/index.html'));
  res.render('pages/helpdesk');       
});

router.post('/createTicket', function(req,res){
  console.log("create ticket is called from email support");
  console.log(req.body);
  console.log("and the details as follows ==>");
  console.log("username is ==> "+req.body.userName);
  console.log("message is ==>"+req.body.message);
  var json = {
          name :   req.body.userName,
          email :  req.body.userEmail,
          subject: 'Demo Subject',
          message:  req.body.message,
          topicId : req.body.topicId,
        };

        var options = {
          url: 'http://domainname/iprhelpdesk/upload/api/http.php/tickets.json',
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'X-API-Key' : 'API-key'
      },  
      json:json
    };

   request(options, function(err, res, body) {
     if (res && (res.statusCode === 200 || res.statusCode === 201)) {
       console.log("response is ==>");
       console.log(res);
     }
     else {
      console.log("error is "+err+ " = and reponse code is ="+res.statusCode );
     }
    });
   res.render('pages/message');   
});

following is the output of the console

下面是控制台的输出

create ticket is called from email support { '{"topicId":{"Id":12,"name":"Basic IPR Query"},"message":"i want to know about ipr","userName":"Pawan Patil","country":"IN","userEmail":"pawanpatil.rocks@gmail.com","contact":"09665714555"}': '' } and the details as follows ==> username is ==> undefined message is ==>undefined POST /createTicket 200 21.161 ms - 104 error is null = and reponse code is =400

创建票由邮件支持{'{"topicId":{"Id":12,"name":"Basic IPR Query"},"message":"i want to know about IPR ","userName":"Pawan Patil","country"

and this is what chrome is sending as a form data

这就是chrome作为表单数据发送的内容

{"topicId":{"Id":12,"name":"Basic IPR Query"},"message":"i want to know about ipr","userName":"Jon Snow","country":"IN","userEmail":"test@test.com","contact":"0123456789"}:

{"topicId":{"Id":12,"name":"Basic IPR Query" "," message":"i want to know about IPR ","userName":"Jon Snow","country":"IN","userEmail":"test@test.com","contact" contact":"0123456789"}:

Request header is

请求头

Content-Type:application/x-www-form-urlencoded

内容类型:应用程序/ x-www-form-urlencoded

everything seems to be perfect but still, I am getting

一切似乎都很完美,但我还是得到了

undefined

未定义的

when I write console.log("username is ==> "+req.body.userName); in my code. please help me out

当我写控制台。日志(“用户名= = > " + req.body.userName);在我的代码。请帮我出

2 个解决方案

#1


2  

I have solved the problem from changing

我已经解决了这个问题。

Content-Type:application/x-www-form-urlencoded

内容类型:应用程序/ x-www-form-urlencoded

header to the

头的

Content-Type: application/json

内容类型:application / json

#2


0  

Move those:

把这些:

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

Before this:

在此之前:

app.use(app.router)

So your bodyParser need to be initialized before the router. I also have read that using this:

因此,您的bodyParser需要在路由器初始化之前进行初始化。我也读过这样的文章:

app.use(require('connect').bodyParser());

Insted of:

本月的:

app.use(express.bodyParser());

May also fix the problem with undefined req.body

还可以修复未定义req.body的问题吗

And one more thing to try is:

还有一件事需要尝试:

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

Set extended to be false instead of true

将扩展为false而不是true

#1


2  

I have solved the problem from changing

我已经解决了这个问题。

Content-Type:application/x-www-form-urlencoded

内容类型:应用程序/ x-www-form-urlencoded

header to the

头的

Content-Type: application/json

内容类型:application / json

#2


0  

Move those:

把这些:

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

Before this:

在此之前:

app.use(app.router)

So your bodyParser need to be initialized before the router. I also have read that using this:

因此,您的bodyParser需要在路由器初始化之前进行初始化。我也读过这样的文章:

app.use(require('connect').bodyParser());

Insted of:

本月的:

app.use(express.bodyParser());

May also fix the problem with undefined req.body

还可以修复未定义req.body的问题吗

And one more thing to try is:

还有一件事需要尝试:

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

Set extended to be false instead of true

将扩展为false而不是true