2

I am getting the following error when trying to write to Firestore. This is done in JavaScript(React).Can anyone tell what is this and how can I fix this?

@firebase/firestore: Firestore (8.3.1): RestConnection Commit failed with error:  {"code":"failed-precondition","name":"FirebaseError"} url:  https://firestore.googleapis.com/v1/projects/{project name}/databases/(default)/documents:commit request: {"writes":[{"update":{"name":"projects/{project name}/databases/(default)/documents/teams/T22yKl1ERQSlfuZNitrvs2vRjSJ2/team-analytics/T22yKl1ERQSlfuZNitrvs2vRjSJ2-Dec-22-2021","fields":{"homePageViews":{"integerValue":"3"},"timeModified":{"timestampValue":"2021-12-22T09:32:00.000000000Z"}}},"updateMask":{"fieldPaths":["homePageViews","timeModified"]},"currentDocument":{"updateTime":"2021-12-22T09:23:08.916511000Z"}}]}

My code that is trying to access Firestore is shown below:

return db.runTransaction(async (transaction) => {
    const analyticsDoc = await transaction.get(analyticsReference);

    if (analyticsDoc.exists) {
        const analytics: any = analyticsDoc.data();
        return transaction.update(analyticsReference, { homePageViews: analytics.homePageViews + 1, timeModified: getCurrentDateTime() });
    }
    const newAnalytics: AnalyticsObject = {
        totalViews: 0,
        homePageViews: 1,
        timeModified: getCurrentDateTime(),
    };
    return transaction.set(analyticsReference, newAnalytics);
});

I am also getting the following error in my console:

POST https://firestore.googleapis.com/v1/projects/optimx-sports/databases/(default)/documents:commit 400

Edit: After more digging in, I am thinking it might be because I am sending 2 transactions to the same document simultaneously. Is it possible that this error is because of this?

Monali Ghotekar
  • 359
  • 1
  • 7
Vishisht Tiwari
  • 101
  • 1
  • 8
  • How are you trying to access Firestore? please share code and details so we can provide a better solution. – Monali Ghotekar Mar 24 '22 at 12:17
  • Added code and some more details above – Vishisht Tiwari Mar 24 '22 at 17:49
  • @MonaliGhotekar please let me know if you need any more info – Vishisht Tiwari Mar 25 '22 at 08:57
  • Are you trying to get analytic data as mentioned in code "homePageViews: analytics.homePageViews"? – Monali Ghotekar Mar 25 '22 at 10:34
  • Its a transaction which fetches the current analytics data, increments by 1 and commits it again to firestore – Vishisht Tiwari Mar 25 '22 at 11:13
  • A few points you can check with: In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications. Have a look at https://firebase.google.com/docs/firestore/solutions/counters from the Firestore documentation. You can refer to the support documentation for Firestore https://cloud.google.com/firestore/docs/troubleshooting#latency. Also You can try with the Postman API https://www.postman.com/ to access data. – Monali Ghotekar Mar 25 '22 at 15:13
  • That's what I thought. Alright, I guess that solves it. – Vishisht Tiwari Mar 26 '22 at 11:18
  • I have posted the above comment in the answer section. If my answer addressed your question, please consider accepting and up-voting – Monali Ghotekar Mar 28 '22 at 07:46

3 Answers3

1

Below are a few Points you can check with:

  1. In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications. Have a look at Firestore documentation.

  2. You can refer to the Documentation.

  3. Also You can try with Postman API to access data.

  4. Another way is combining two commits as well.

Monali Ghotekar
  • 359
  • 1
  • 7
0

The issue was that I was sending two transaction commits to one firestore document within a second. The second commit was raising the above error. Fixed it by combining the two commits

Vishisht Tiwari
  • 101
  • 1
  • 8
0

In my case, I was making a transaction commit inside a hook and React was rendering my component twice. This was too much for Firestore to handle.

The solution was to disable strict mode for React:

// index.js

// good 
root.render(<App/>); ✅

// bad
root.render(
  <React.StrictMode>
    <App/>
  </React.StrictMode>
); ❌

References

https://stackoverflow.com/a/65167384/17627866

Bunny
  • 1,180
  • 8
  • 22