我有一个带有成功回调的简单http调用,但是当我尝试返回原始参数时,react会返回一个未处理的promise promise rejection错误,这会导致我的调度失败:
export function deleteFriendRequest(userId) {
return dispatch => {
// dispatch(deleteFriendRequestSuccess({}, userId))
request.del({dispatch, path: `/friends/${userId}`, body: {}, deleteFriendRequestSuccess, deleteFriendRequestFailure, initialData: userId})
}
}
function deleteFriendRequestSuccess(payload, initialData) {
console.log('delete friend request success', payload, userId) // this works I get the correct user id
return {
type: FRIENDS_DELETE_FRIEND_REQUEST_SUCCESS,
payload: {sentId: initialData},
}
}
function deleteFriendRequestFailure(error) {
return {
type: FRIENDS_DELETE_FRIEND_REQUEST_FAILURE,
error,
}
}
export function del({dispatch, path, body, success, failure, initialData}) {
const url = API_URL + path
const token = store.getState().auth.token
fetch(url, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'AccessToken': token && token.token ? token.token : null,
'deviceUID': DeviceInfo.getUniqueID(),
},
body: JSON.stringify(body),
})
.then(function (response) {
if (response.status < 200 || response.status > 299) {
throw ({errorMessage: 'Invalid response'}, response)
} else {
return response
}
})
.then(response => response.json())
.then(checkForResponseError)
.then(json => {
dispatch(success(json, initialData))
})
.catch(err => {
dispatch(failure(err))
})
}
导致:
Possible Unhandled Promise Rejection (id: 0):
failure is not a function
TypeError: failure is not a function
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:84194:10
at tryCallOne (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:29539:8)
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:29625:9
at JSTimersExecution.callbacks.(anonymous function) (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:12854:13)
at Object.callTimer (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:12686:1)
at Object.callImmediatesPass (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:12744:19)
at Object.callImmediates (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:12759:25)
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:11613:43
at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:11518:1)
at MessageQueue.__callImmediates (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false&hot=true:11613:1)
关于如何解决这个问题的任何想法?
答案 0 :(得分:1)
传递一个选项对象,然后在函数头中进行解构。由于您以不同的名称传递它,因此未分配NSNotificationCenter.defaultCenter().addObserver(self,
selector:#selector(updateNowPlayingInfo),
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification,
object: musicPlayer)
var,因此当您在promise的failure
中调用failure
函数时,它将失败。这使得catch
调用的结果(也是一个承诺)因该错误而失败。由于您对catch
调用的结果不执行任何操作,因此您会收到未处理的承诺拒绝通知。
您的catch
函数也未正确传递,这可能是导致首先到达success
的原因。
您的功能应如下所示:
catch