【问题标题】:Firebase update method produces Promise RejectionFirebase 更新方法产生 Promise Rejection
【发布时间】:2016-11-05 13:33:32
【问题描述】:

我正在向我的 Firebase 提出关于我的 React Native 应用程序的更新请求,并考虑到了 redux。

这是我的 redux sn-p

export function buyTicket(eventID) {
  const { currentUser } = firebase.auth();
  return (dispatch) => {
    firebase.database().ref(`/Users/${currentUser.uid}/joinedEvent`).update({ [eventID]: true })
      .then(() => dispatch({ type: BUY_TICKET_SUCCESS }))
      .catch(() => dispatch({ type: BUY_TICKET_FAIL }));
  };
};

当调用 buyTicket 函数时,应该只需要 then() 方法,但 then() 和 catch() 都会被调用。

根据 Firebase 文档,update() 产生一个承诺,但它是可选的。

这是我遇到的错误

这是我的减速器

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case PULL_EVENT_DATA:
      return action.payload;
    case PULL_TRENDING_DATA:
      return action.payload;
    case BUY_TICKET_SUCCESS:
      return {
        message: 'Yay, see you there!'
      }
    case BUY_TICKET_FAIL:
      return {
        message: 'shit'
      }
    default:
      return state;
  }
}

也许控制台日志可能会提供线索?

【问题讨论】:

  • 你确定渔获回来了吗? Object.assign是第一个警告线,可能和你的reducer有关?
  • 我已经修改了我的减速器,但它看起来对我来说还不错......@Jan

标签: javascript reactjs firebase firebase-realtime-database redux


【解决方案1】:

正如 Jan 指出我遗漏的部分,我能够重新定位错误并纠正错误。

我犯的错误是可变性。我在制作应用程序时没有考虑可变性的概念,因此出现了错误。

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case PULL_EVENT_DATA:
      return action.payload;
    case PULL_TRENDING_DATA:
      return action.payload;
    case BUY_TICKET_SUCCESS:
      return { ...state, message: action.payload}
    case BUY_TICKET_FAIL:
      return { ...state, message: action.payload}
    default:
      return state;
  }
}

您可能会发现更多关于可变性here

【讨论】:

    猜你喜欢
    • 2017-01-25
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多