Home > Blockchain >  'RestConnection Commit failed with error' when trying to write to firestore
'RestConnection Commit failed with error' when trying to write to firestore

Time:03-26

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?

CodePudding user response:

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

  • Related