Получение ошибки при попытке использовать firestore.runTransaction изact-native-firebase - PullRequest
0 голосов
/ 17 мая 2018

Я следовал примерам https://rnfirebase.io/docs/v4.1.x/firestore/transactions и https://blog.invertase.io/getting-started-with-cloud-firestore-on-react-native-b338fb6525b9, но ни один из этих вариантов не работает:

import rnFirebase, {firestore} from 'react-native-firebase'

rnFirebase.firestore().runTransaction(async tx => {
  // nothing yet
} // undefined is not an object (evaluating 'ref[method].apply')

firestore.runTransaction(async tx => {
  // todo
} // undefined is not an object (evaluating '_reactNativeFirebase.firestore.runTransaction')

firestore().runTransaction(async tx => {
  // doesn't matter
} // ... is not a function ... is undefined

Вот полный код, который я использую ...

Инициализация хранилища редуксов:

// ./redux/index.js

import {createStore, applyMiddleware, combineReducers, compose} from 'redux'
import {composeWithDevTools} from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'
import RNFirebase from 'react-native-firebase'
import {firebaseReducer, reactReduxFirebase, getFirebase} from 'react-redux-firebase'
import {reduxFirestore, firestoreReducer, getFirestore} from 'redux-firestore'

import lists from './modules/lists'

// Firebase config
const firebaseConfig = {
  apiKey: "XXX",
  authDomain: "XXX",
  databaseURL: "XXX",
  projectId: "xxx",
  storageBucket: "xxx.appspot.com",
  messagingSenderId: "xxx"
}

// react-redux-firebase options
const rrfConfig = {
  userProfile: 'users', // firebase root where user profiles are stored
  enableLogging: false, // enable/disable Firebase's database logging
  attachAuthIsReady: true, // attaches auth is ready promise to store
  // workaround for https://github.com/invertase/react-native-firebase/issues/431
  enableRedirectHandling: false,
  allowMultipleListeners: true,
}

const reactNativeFirebaseConfig = {
  debug: true
}

export default (initialState = {}) => {
  // initialize firebase
  const firebase = RNFirebase.initializeApp(reactNativeFirebaseConfig)
  // Initialize Cloud Firestore through Firebase
  RNFirebase.firestore()

  const reducer = combineReducers({
    lists,
    firebase: firebaseReducer,
    firestore: firestoreReducer,
  })

  const middleware = [
     // make getFirebase available in third argument of thunks
    thunkMiddleware.withExtraArgument({getFirebase, getFirestore}),
  ]

  return createStore(
    reducer,
    initialState,
    compose(
      reactReduxFirebase(firebase, rrfConfig), // pass initialized react-native-firebase app instance
      reduxFirestore(firebase),
      composeWithDevTools(applyMiddleware(...middleware))
    )
  )
}

А мой создатель действий выглядит как

// ./redux/modules/lists.js

import RNFirebase, {firestore} from 'react-native-firebase'

// ...

// Async Action Creator
export const addToList = (attraction, listKey) => {
  return async (dispatch, getState, {getFirestore}) => {
    const reduxFirestore = getFirestore() // provided by redux-firestore... this gives the same behavior as using react-native-firebase directly...

    try {
      const ref = `lists/${listKey}`
      const res = await reduxFirestore.get(ref)
      const {attractionsIndex} = res.data() // this works fine

      RNFirebase.firestore().runTransaction(async tx => {
        const doc = await tx.get(ref) // line 139... this explodes

        if (doc.exists) {
          console.warn('exists!')
        } else {
          console.warn('does not exist!')
        }
      })

    } catch(err) {
      console.error(err)
    }
  }
}

, что приводит к ошибке

undefined is not an object (evaluating 'ref[method].apply')

wrapInDispatch
    actions.js:57:21
_callee3$
    lists.js:139:36
tryCatch
    runtime.js:62:44
invoke
    runtime.js:296:30
tryCatch
    runtime.js:62:44
invoke
    runtime.js:152:28
<unknown>
    runtime.js:162:19
tryCallOne
    core.js:37:14
<unknown>
    core.js:123:25
_callTimer
    JSTimers.js:154:6
_callImmediatesPass
    JSTimers.js:202:17
callImmediates
    JSTimers.js:470:11
__callImmediates
    MessageQueue.js:329:4
<unknown>
    MessageQueue.js:147:6
__guardSafe
    MessageQueue.js:316:6
flushedQueue
    MessageQueue.js:146:17
...