axios返回“完全”承诺

aij0ehis  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(173)

有人能告诉我为什么Axios服务函数返回以下信息吗:
对象在服务功能中看起来很好。

Promise {<fulfilled>: {…}}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object

下面是调用:

useEffect(() => {
    setData(playlistService.getPlaylists);
  }, []);
  console.log(data);

和功能:

const config = {
  headers: {
    'Content-Type': 'application/json',
  },
  withCredentials: true,
};

const getPlaylists = async () => {
  try {
    const res = await axios.get(`${API_SONGS_URL}/`, config);
    console.log('RES ', res);
    return res;
  } catch (error) {
    if (error.response) {
      return error.response.data;
    }
  }
};
mnowg1ta

mnowg1ta1#

这可能行得通

useEffect(() => {
    const fetchPlayListAsync = async () => {
        const response = await playlistService.getPlaylists();
        setData(response)
    }
   fetchPlayListAsync()
}, []);

您可以添加对提取数据的适当检查

相关问题