React-redux-firebase: getFirestore in redux-thunk

Created on 24 Oct 2019  路  6Comments  路  Source: prescottprue/react-redux-firebase

How to access firestore to read and write data in redux thunk. Only getFirebase is available, not getFirestore in the latest release v3.

Most helpful comment

I'm working through a similar issue right now (while migrating from v2 to v3 of react-redux-firebase), but seeing (I think) that the instance of firestore exposed through getFirebase().firestore() (in v3) doesn't have access to the same statics as the one that getFirestore() exposed (in v2).

Specifically, statics like firestore.Timestamp and firestore.FieldValue don't seem to be available on the firestore instance exposed through getFirebase().firestore().

In V2, this was working:

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase, getFirestore }) => {
    const firestore = getFirestore();
    const convertedDate = firestore.Timestamp.fromDate(someDate);
    // ...
  };
};

In V3:
Does not work, with error Uncaught TypeError: Cannot read property 'fromDate' of undefined

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase }) => {
    const firestore = getFirebase().firestore();
    const convertedDate = firestore.Timestamp.fromDate(someDate);
    // ...
  };
};

Workaround that does work:

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase }) => {
    const convertedDate = getFirebase().firestore.Timestamp.fromDate(someDate); // Access the service namespace directly instead of from the instance
    // ...
  };
};

Is this expected @prescottprue ? Thanks!

All 6 comments

You should be able to do getFirebase().firestore() to access it for now, but I will look into why getFirestore isn't exported

Just remember that getFirestore is actually exported from redux-firestore, so if you want to add it to your thunk integration you would grab it from there. Thanks for reaching out

I request you to please update docs because I can only see getfirebase, not getfirestore, even after doing search in docs. It will be helpful for those who just started using this library for the very first time. I started using it around 1 month back so I thought you have removed it in v3 since it was not working for me at that moment. Also, thanks for solution, now it works.

@prescottprue I can confirm that getFirebase().firestore() works when using redux thunk

@petermarks12 you can replease getFirestore with the above for now

I'm working through a similar issue right now (while migrating from v2 to v3 of react-redux-firebase), but seeing (I think) that the instance of firestore exposed through getFirebase().firestore() (in v3) doesn't have access to the same statics as the one that getFirestore() exposed (in v2).

Specifically, statics like firestore.Timestamp and firestore.FieldValue don't seem to be available on the firestore instance exposed through getFirebase().firestore().

In V2, this was working:

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase, getFirestore }) => {
    const firestore = getFirestore();
    const convertedDate = firestore.Timestamp.fromDate(someDate);
    // ...
  };
};

In V3:
Does not work, with error Uncaught TypeError: Cannot read property 'fromDate' of undefined

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase }) => {
    const firestore = getFirebase().firestore();
    const convertedDate = firestore.Timestamp.fromDate(someDate);
    // ...
  };
};

Workaround that does work:

export const someAction = (someDate) => {
  return (dispatch, getState, { getFirebase }) => {
    const convertedDate = getFirebase().firestore.Timestamp.fromDate(someDate); // Access the service namespace directly instead of from the instance
    // ...
  };
};

Is this expected @prescottprue ? Thanks!

I confirm that getFirebase().firestore() is a different instance than the one exposed via useFirestore for example.

For example firestore().add('/some-collection', document) does not work (must use firstore().collection('some-collection').add(document).

Was this page helpful?
0 / 5 - 0 ratings