我是JS和react的新手,但我正試圖將一個字串傳遞給我的API,然后對該字串進行分析并傳回一個情感評分。然而,當我傳遞資料時,雖然在API的控制臺中,字串出現在 "主體 "中,但API指出 "描述 "是未定義的,導致API出現錯誤。
api.js
const express = require('express')。
const app = express()
app.use(express.json()) 。
app.post('/', function (req, res) {
console.log('body is:', req.body) 。
const {description} = req.body;
console.log('Description is:'/span>, description)。
var Analyzer = require('natural').SentimentAnalyzer。
var stemmer = require('natural').PorterStemmer。
var analyzer = new Analyzer("English", stemmer, "afinn") 。
const sentiment = analyzer.getSentiment(description.split)。
console.log('Sentiment is: ', sentiment);
res.json(JSON.stringify({sentiment})
})
app.listen(3000, () => {
console.log('listening on port 3000')。
});
diary.js包含post函式和api呼叫
async function makePostRequest (diaryText) {
let payload = { Description: diaryText };
let res = await axios.post('http://localhost:3000/'/span>, payload);
let data = res.data;
console.log("This is the data returned in the post function:"/span>,data)。
}
makePostRequest("testText").then(response => {
console.log("從函式呼叫回傳的資料:", response)。
}).then(error => {
console.error(error.response.data)。
});
這是執行上述代碼時在控制臺中的輸出:
body is: { description: 'testText' }
Description is: undefined is.
TypeError: 不能讀取屬性'split' of undefined。
在/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/api. js:14:54。
在Layer。 handle [as handle_request] (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/layer。 js:95:5)
在 next (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/route。 js:137:13)
在Route。 dispatch(/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/route。 js:112:3)
在Layer。 handle [as handle_request] (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/layer。 js:95:5)
在/Users/ddavies/Desktop/Desktop。 title class_">Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index. js:281:22。
在Function。 process_params (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index. js:335:12)
在 next (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index. js:275:10)
在/Users/ddavies/Desktop/Desktop。 title class_">Desktop Storage/programming/MSc_App/node_modules/body-parser/lib/read. js:130:5。
在 invokeCallback (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/raw-body/index. js:224:16)
uj5u.com熱心網友回復:
物件鍵是敏感的,Description =/= description
const {Description} = req.body;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/326904.html
標籤:
下一篇:獲取ComboBox中的值的ID
