我在這里找到了不同的解決方案,但沒有一個對我有用。我有這個簡單的代碼:
const Element = () => {
async function getEndData() {
const data = (await getEnd());
return data;
}
}
const getEnd = async () => {
return await axios.get('http://localhost:8080/end').then(res => res.data);
}
當我呼叫. _ _ _ _ getEndData()我也嘗試呼叫直接getEnd()洗掉then(),只回傳資料,但什么也沒有。res.data如果我在console.log中列印,它將回傳我需要的正確值。
uj5u.com熱心網友回復:
這應該作業:
const Element = () => {
async function getEndData() {
const data = await getEnd();
return data;
}
}
const getEnd = async () => {
const response = await axios.get('http://localhost:8080/end');
return response;
}
uj5u.com熱心網友回復:
我不確定您是否以正確的方式進行操作。你可以試試這個:
const Element = () => {
return async function getEndData() {
const data = await getEnd();
return data;
}
}
const getEnd = () => {
return new Promise((resolve, reject) => {
axios.get('http://localhost:8080/end')
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err);
})
})
}
另外,如果您不回傳任何內容,元素函式的用途是什么。
uj5u.com熱心網友回復:
你的 getEndData() 回傳一個 promise 。添加 await 或然后在您收到 getEndData() 回應的地方。
// const Element = () => {
async function getEndData() {
const data = (await getEnd());
return data;
}
// }
const getEnd = async () => {
return await axios.get('http://localhost:8080/end').then(res => res);
}
async function callEndData(){
let x = await getEndData()
console.log(x)
}
callEndData()
因為它正在回傳承諾并且您沒有使用等待,或者它顯示承諾待處理
為什么需要 Element() ?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442498.html
標籤:javascript 节点.js 反应 打字稿 axios
上一篇:點擊時激活串列
