大家好,我正在使用 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
即使使用 body-parser,node.js post 方法 req.body 也未定義
繁華開(kāi)滿天機(jī)
2023-08-24 18:25:24