[cloud-firestore] Unable to set in a Firestore transaction if the collection does not exist. #7433
-
Hi, struggling a lot with this. IssueThe following transaction fails if the root collection Eventually, the CodePath: Future update(String itemId)
{
final ref = db.collection('unpublished').doc('cityId').collection('items').doc('itemId');
await db.runTransaction((transaction) async {
// Gets the document if any
final item = await transaction.get(ref);
// Update just some fields
if (document.exists) {
// Make sure there is no pending changes
final hasChanged = item.updatedDate > lastUpdatedDate;
// In case of any changes
if (hasChanged) throw 'Has changes';
// Otherwise, update
final json = ...;
transaction.set(item, json, SetOptions(merge: true));
}
// Create the document with all fields
else {
final json = ...;
transaction.set(item, json); <-- this fails, due to exhausted retries
}
});
} I figured out the transaction was failing (retrying all time) if the root collection or the sub-collection did not exist. Using a different structure with a flag field and avoiding the Any help is much appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Never mind. It seems to be happening only with the emulator. |
Beta Was this translation helpful? Give feedback.
Never mind.
It seems to be happening only with the emulator.