Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

355
Visualizações
How do you create a new Firestore document within a transaction

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'?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

With regard to the wider question of how you add records within a transaction, the answer is that you would use pretty much the same code as you'd use outside a transaction.

So whereas outside a transaction you would create a new document with a data item as its identifier with the following Web Version 9 code:

        const myDocRef = doc(db, "myCollection", myDocId);
        await setDoc(myDocRef, myDocData);

Inside a transaction, you use exactly the same pattern with the one exception that setDoc() is replaced by transaction.set() and the whole operation (obviously) is surrounded by a runTransaction(db, async (transaction) => { instruction:

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

        const myDocRef = doc(db, "myCollection", myDocId);
        await transaction.set(myDocRef, myDocData);

    }).catch((error) => {
        alert(`Oops - transaction failed - error is : ${error}`);
    });

Similarly, the pattern used to create a document with an automatically-generated id:

        const myCollectionRef = collection(db, "myCollection");
        const myDocRef = doc(myCollectionRef)
        await setDoc(myDocRef, myDocData);

is replaced within a transaction by

        const myCollectionRef = collection(db, "myCollection");
        const myDocRef = doc(myCollectionRef)
        await transaction.set(myDocRef, myDocData);

Note that if you find you need to find the value that's be assigned to your automatically-generated id, this is available as myDocRef.id

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda