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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

繁華開滿天機 2023-08-24 18:25:24
大家好,我正在使用 Node js 來使用對話流聊天機器人,我正在嘗試從 http 請求 post 方法中獲取參數(shù)我為此使用了 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);這是我的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ā)送以下請求時收到的錯誤dialogFlowRoutes.js:17                    text: req.body.text,                                   ^TypeError: Cannot read property 'text' of undefined
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

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


在對 body 進行操作之前,您必須對其進行解析。初始化 bodyParser 后移動路由中間件,如下所示:


app.use(bodyParser.json());

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

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


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號