0
我有一個簡單的http調用成功回調,但是當我嘗試返回原始參數時,反應返回未處理的promise拒絕錯誤,導致我的調度失敗:react-native,關閉變量導致動作失敗
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)
如何解決這個任何想法?
啊,對不起,我沒有在片段中添加deleteFriendRequestFailure功能上面,已經很晚了,我累了,它被定義的,但同樣的錯誤是拋出,我將編輯答案以包含它 – ospfranco
更新的答案是否幫助您解決問題?如果是這樣,請將其標記爲解決方案。如果沒有,讓我知道什麼是錯的,所以我可以修復它。 – DDS
感謝您的評論,我沒有正確更新代碼,而我最終得到了與您的代碼完全相同的代碼(但我直到現在才查看更新的答案),我不明白爲什麼通過目標導致參數在封閉中保持可用,任何澄清都將不勝感激。 – ospfranco