Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js post方法req.body未定义,即使使用body解析器_Javascript_Node.js_Express_Body Parser - Fatal编程技术网

Javascript node.js post方法req.body未定义,即使使用body解析器

Javascript node.js post方法req.body未定义,即使使用body解析器,javascript,node.js,express,body-parser,Javascript,Node.js,Express,Body Parser,大家好,我正在使用node js使用dialogflow聊天机器人 我正在尝试从http请求post方法获取参数 我使用了postman,是的,我在标题中将内容类型设置为json,我的请求正文有以下代码: { "text":"hello" } 以及以下链接 http://localhost:5000/api/df_text_query 我有以下index.js文件: const express = require('express'); const bo

大家好,我正在使用node js使用dialogflow聊天机器人

我正在尝试从http请求post方法获取参数

我使用了postman,是的,我在标题中将内容类型设置为json,我的请求正文有以下代码:

{
"text":"hello"
}
以及以下链接 http://localhost:5000/api/df_text_query

我有以下index.js文件:

const express = require('express');
const bodyParser = require('body-parser');


const app = express();

require('./routes/dialogFlowRoutes')(app);


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

app.get('/',(req , res)=>{

res.send({'hello':'Johnny'});

});


const PORT = process.env.port || 5000;

app.listen(PORT);
const dialogflow = require('dialogflow');
 const config = require('../config/keys');
 const sessionClient = new dialogflow.SessionsClient();
 const sessionPath = sessionClient.sessionPath(config.googleProjectID, config.dialogFlowSessionID);
    module.exports = app => {
    app.get('/', (req, res) => {
        res.send({ 'hello': 'world!' })
        
    });
    app.post('/api/df_text_query', async (req, res) => {
        console.log(req.body)
        const request = {
            session: sessionPath,
            queryInput: {
                text: {
                    text: req.body.text,
                    languageCode: config.dialogFlowSessionLanguageCode
                }
            }
        };

    let responses = await sessionClient
        .detectIntent(request);

    res.send(responses[0].queryResult)
    });

app.post('/api/df_event_query', (req, res) => {
    res.send({ 'do': 'event query' })
});
}
这是my dialogflowRoutes.js文件:

const express = require('express');
const bodyParser = require('body-parser');


const app = express();

require('./routes/dialogFlowRoutes')(app);


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

app.get('/',(req , res)=>{

res.send({'hello':'Johnny'});

});


const PORT = process.env.port || 5000;

app.listen(PORT);
const dialogflow = require('dialogflow');
 const config = require('../config/keys');
 const sessionClient = new dialogflow.SessionsClient();
 const sessionPath = sessionClient.sessionPath(config.googleProjectID, config.dialogFlowSessionID);
    module.exports = app => {
    app.get('/', (req, res) => {
        res.send({ 'hello': 'world!' })
        
    });
    app.post('/api/df_text_query', async (req, res) => {
        console.log(req.body)
        const request = {
            session: sessionPath,
            queryInput: {
                text: {
                    text: req.body.text,
                    languageCode: config.dialogFlowSessionLanguageCode
                }
            }
        };

    let responses = await sessionClient
        .detectIntent(request);

    res.send(responses[0].queryResult)
    });

app.post('/api/df_event_query', (req, res) => {
    res.send({ 'do': 'event query' })
});
}
这是我发送以下请求时遇到的错误

dialogFlowRoutes.js:17
                    text: req.body.text,
                                   ^

TypeError: Cannot read property 'text' of undefined

初始化中间件的顺序很重要

在对主体执行操作之前,必须对其进行分析。初始化bodyParser后移动路由中间件,如下所示:

app.use(bodyParser.json());
use(bodyParser.urlencoded({extended:true}))
要求(“./路线/对话框流程”)(应用程序);