第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

即使使用 body-parser,node.js post 方法 req.body 也未定義

即使使用 body-parser,node.js post 方法 req.body 也未定義

大家好,我正在使用 Node js 來(lái)使用對(duì)話流聊天機(jī)器人,我正在嘗試從 http 請(qǐng)求 post 方法中獲取參數(shù)我為此使用了 postman,是的,我確實(shí)在標(biāo)頭中將內(nèi)容類型設(shè)置為 json,我的請(qǐng)求正文有以下代碼:{"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);這是我的dialogflowRoutes.js 文件: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' })});}這是我發(fā)送以下請(qǐng)求時(shí)收到的錯(cuò)誤dialogFlowRoutes.js:17                    text: req.body.text,                                   ^TypeError: Cannot read property 'text' of undefined
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

初始化中間件的順序很重要。


在對(duì) body 進(jìn)行操作之前,您必須對(duì)其進(jìn)行解析。初始化 bodyParser 后移動(dòng)路由中間件,如下所示:


app.use(bodyParser.json());

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

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


查看完整回答
反對(duì) 回復(fù) 2023-08-24
  • 1 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)