那是我對API 呼叫的原始回應,我試圖用一個.forEach
const app = express();
const axios = require('axios')
jobList = [];
app.get('/getAPIResponse', function(req, res) {
async function myFunction() {
axios.get('https://api...')
//res.json(body)
.then((result) => {
result.results.forEach((element) => {
console.log(element)
this.jobList.push(element)
});
})
}
myfunction();
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
但問題是,它讓我的終端出現了一些錯誤
(rejection id: 2)
(node:81826) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at /Users/mac/Desktop/alternatics_test/index.js:22:24
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:81826) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:81826) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at /Users/mac/Desktop/alternatics_test/index.js:22:24
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:81826) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
盡可能清楚的results是api中的關鍵字,我想從中獲取每個結果塊以插入我的“jobList”
我到處尋找,這forEach似乎是正確的技術......
uj5u.com熱心網友回復:
您正在使用模塊axios
這將回傳帶有axios 回應模式的回應
所以你必須得到這樣的結果result.data.results然后你可以使用forEach函式
uj5u.com熱心網友回復:
采用result.data.results.forEach
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444653.html
標籤:javascript 节点.js 表示
