fetch请求catch里面的回调函数报错 reactjs

catch里面回调函数为什么提示不是一个函数?请求接口最后也会进来catch回调里面,不知为啥会报错
Uncaught (in promise) TypeError: catchCallback is not a function

图片描述

//fetchEX.js
export function fetchPost(url, json, callback, catchCallback) {
  fetch(url,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'text/plain',
        'username': cookie('username'),
        'token': cookie('token'),
      },
      body: json,
    })
    .then(response => {
        if (response.ok) return response.json();
        else return Promise.reject(response);
      }
    )
    .then(data => callback(data))
    .catch(err => {
      console.log(err,'err')//TypeError: Failed to fetch
      return catchCallback(err)//Uncaught (in promise) TypeError: catchCallback is not a function
    });
}

export function fetchMs(url, json, callback, catchCallback) {
  fetchPost(`http://$xxx.xx.xx.xx:10034` + url, JSON.stringify(json), callback, catchCallback);
}

//index.js
fetchMs("/xxx/xxxx/xxx/xxx", {
   moduleId: 'xxx',
}, res => {
    
},(error) => {
  console.log(error,'==test')//同样进来也会打印出 TypeError: Failed to fetch
});
阅读 3.7k
1 个回答

写发改成如下

catchCallback && catchCallback()

或者

catchCallback 参数这是默认为一个function

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题