react-native,closure变量导致操作失败

时间:2016-08-23 02:04:31

标签: javascript react-native redux

我有一个带有成功回调的简单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)

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

传递一个选项对象,然后在函数头中进行解构。由于您以不同的名称传递它,因此未分配NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(updateNowPlayingInfo), name: MPMusicPlayerControllerPlaybackStateDidChangeNotification, object: musicPlayer) var,因此当您在promise的failure中调用failure函数时,它将失败。这使得catch调用的结果(也是一个承诺)因该错误而失败。由于您对catch调用的结果不执行任何操作,因此您会收到未处理的承诺拒绝通知。

您的catch函数也未正确传递,这可能是导致首先到达success的原因。

您的功能应如下所示:

catch