Javascript 解决未处理的拒绝(TypeError):回调不是函数

Javascript 解决未处理的拒绝(TypeError):回调不是函数,javascript,Javascript,我正在编写一个函数,它进行API调用并从API调用返回数据。然后它必须转到一个调用callback()函数的新页面。当我运行代码时,我确实会导航到新页面,但几毫秒后,我会得到一个错误,如下面的屏幕截图所示。我使用的代码是: export function editPost(id, callback) { const request = axios.get(`${API}/posts/${id}`,{headers}); return dispatch => { re

我正在编写一个函数,它进行API调用并从API调用返回数据。然后它必须转到一个调用callback()函数的新页面。当我运行代码时,我确实会导航到新页面,但几毫秒后,我会得到一个错误,如下面的屏幕截图所示。我使用的代码是:

export function editPost(id, callback) {
  const request = axios.get(`${API}/posts/${id}`,{headers});
    return dispatch => {
      return request.then((data) => {
        callback();
        console.log(data);
        dispatch({
          type: EDIT_POST,
          payload: data
        })
      })
    }
}
我收到的错误消息如下所示:


有人能帮我吗?

在调用
editPost
时,它还需要第二个参数(回调函数)


很高兴它帮助了你

调用
editPost
时,它还需要第二个参数(回调函数)


很高兴它帮助了你

您可以在调用回调之前检查回调是否已传递

if (typeof callback === 'function') {
    callback();
}

您可以在调用回调之前检查回调是否已传递

if (typeof callback === 'function') {
    callback();
}

在这里,您需要直接使用

<childComponent {state1}={state1} {setState1}={setState1}/>

在这里,您需要直接使用

<childComponent {state1}={state1} {setState1}={setState1}/>

你如何调用
editPost
函数?你在哪里定义了
callback()
函数?@ricky我这样调用它:`componentDidMount(){const{id}=this.props.match.params;this.props.editPost(id);console.log(id)}`在调用
editPost
时,它需要第二个参数(回调函数)“还有,”瑞奇,这就是问题所在。直到你提到我才注意到。非常感谢。你能把它添加到答案中,这样我就可以接受你的答案了。再次感谢:)您如何调用
editPost
函数?您在哪里定义了
callback()
函数?@ricky我这样调用它:`componentDidMount(){const{id}=this.props.match.params;this.props.editPost(id);console.log(id)}`调用
editPost
时,它需要第二个参数(回调函数)也是。@ricky,这就是问题所在。直到你提到这一点我才注意到。非常感谢。你能把它添加到答案中,这样我就可以接受你的答案了。再次感谢:)