代码正在运行,但我没有得到所需的输出

每当我单击删除按钮时,它都可以正常工作,但我没有得到像“成功删除”这样的输出,它的显示。然后是未定义的。


const deleteThisCategory = (CategoryId) => {

  deleteCategory(CategoryId, user._id, token).then(data => {

    if (data.error) {

      console.log(data.error);

    } else {

      preload();

    }

  });

};


这是删除类别 API 调用


export const deleteCategory = (userId, categoryId , token) => {

  fetch(`${API}/category/${categoryId}/${userId}`, {

    method: "DELETE",

    headers: {

      Authorization: `Bearer ${token}`,

      "Content-Type":"application/json"

    },

   

  })

    .then(response => {

      return response.json();

    })

    .catch(err => console.log(err));

};


潇湘沐
浏览 92回答 1
1回答

繁华开满天机

应该是这样的。deleteCategory 只需要发送承诺。以后无论你在哪里解决,你都必须使用 then。export const deleteCategory = (userId, categoryId , token) => {  return fetch(`${API}/category/${categoryId}/${userId}`, {    method: "DELETE",    headers: {      Authorization: `Bearer ${token}`,      "Content-Type":"application/json"    }  })};const deleteThisCategory = (CategoryId) => {  deleteCategory(CategoryId, user._id, token).then(data => {      preload();    }).catch(err => {        console.log(err);    })};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript