Home > Software engineering >  How do you create a new Firestore document within a transaction
How do you create a new Firestore document within a transaction

Time:02-22

I'm trying to log changes to a collection of customer records. In order to keep things watertight,the log records should obviously be created within a Firestore transaction. I have no problems using transaction.set to apply the customer document changes, but every such change needs to be accompanied by the creation of a new document within my transactionLogs collection. Here, things are going badly wrong The log documents are identified by a timestamp field and when I run the following code: ...

import { initializeApp } from 'firebase/app';
import {
    getFirestore, collection, query, getDoc,
    getDocs, where, orderBy, addDoc, doc, updateDoc, deleteDoc, runTransaction
} from 'firebase/firestore';

var firebaseApp = initializeApp(firebaseConfig);
var db = getFirestore(firebaseApp);

// code to read and validate update to customer documents and to call the following asynch function

function performCustomerUpdate(parameters) {

await runTransaction(db, async (transaction) => {

 // update Customer document and build a transactionLog object

 const newLogRef = collection(db, 'transactionLogs', transactionLog.timeStamp);
 await transaction.set(newLogRef, transactionLog);

});
}

... the transaction.set instruction fails saying something like "Invalid collection reference. Collection references must have an odd number of segments, but transactionsLogs/1645451217221 has 2." In this particular instance, 1645451217221 would have been the value of transactionLog.timesStamp.

Does anyone have advice on what is going on here and how to fix the error? My understanding is that transaction.set will create a new record if the supplied reference doesn't exist, so I ought to be on the right lines. But why does Firestore think that I want to create it in a collection called transactionsLogs/1645451217221? How do I get it the create a reference for a document identified by the string '1645451217221' in a collection called 'transactionsLogs'?

CodePudding user response:

If you are specifying the document ID (and not using the auto-generated IDs) then you must use doc() instead of collection() to create a DocumentReference:

const newLogRef = doc(db, 'transactionLogs', transactionLog.timeStamp);

The collection() function is used create a CollectionReference.

Also checkout: Firestore: What's the pattern for adding new data in Web v9?


My understanding is that transaction.set will create a new record if the supplied reference doesn't exist

If the documents exists, the it'll overwrite the existing document.

  • Related