1 回答

TA貢獻1834條經(jīng)驗 獲得超8個贊
我相信這個nuxtServerInit動作被恰當?shù)卣{用了。我敢打賭,它message的值始終為未定義,因為 Axios.get("http://localhost:5000/api/home")返回的 promise 沒有屬性data。您必須先等待結果,然后獲取它的數(shù)據(jù)。
// note extra parentheses
const message = await (Axios.get("http://localhost:5000/api/home")).data;
此外,您可能希望將 axios 調用包裝在 try-catch 中,否則如果調用失敗,您將獲得帶有堆棧跟蹤的默認 Nuxt 錯誤頁面。
async nuxtServerInit ({ commit }: any) {
try {
const message = (await Axios.get('http://localhost:5000/api/home')).data;
console.log(message);
commit('setMessage', message); // put name of mutation as parameter + payload
} catch (error) {
// you could redirect to custom error page for instance
console.error(error);
}
}
添加回答
舉報