gpt4 book ai didi

javascript - 无法解构 'data' 的属性 '(intermediate value)',因为它未定义

转载 作者:行者123 更新时间:2023-12-04 12:09:40 25 4
gpt4 key购买 nike

此错误在控制台中,它会阻止应用程序运行,如果有人可以提供帮助,我根本找不到错误?它是一个 MERN 应用程序
有问题的代码

export const getPosts = () => async (dispatch) => {
try {
const { data } = await api.fetchPosts();

dispatch({ type: 'FETCH_ALL', payload: data });
} catch (error) {
console.log(error.message);
}
};
VSC 告诉我 await 不会影响这种表达,它应该作为 fetchPosts 是一个请求?代码如下
export const fetchPosts = () => {
axios.get(url)
}

最佳答案

问题是,虽然 axios.get 可能会返回一个 promise ,fetchPosts您包装它的函数不会返回 axios.get 的 promise 返回:

const fetchPosts = () => {
axios.get(url);
};

const myFetch = fetchPosts();

console.log(myFetch); // will log `undefined`
如果重写 fetchPosts像这样:
export const fetchPosts = () => axios.get(url);
...与 implicit return from your arrow function ,我认为它应该工作。或者,您可以直接返回 axios.get 的结果。 :
const fetchPosts = () => {
return axios.get(url);
};
...但是您的 linter 可能会提示这一点。

关于javascript - 无法解构 'data' 的属性 '(intermediate value)',因为它未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66340174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com