У меня есть документ, который я получаю с таким запросом:
var myPromise = db.collection("games").where("started", "==", false).orderBy("created").limit(1).get()
Мне нужно создать транзакцию, в которой выполняется чтение из этого документа, а затем запись. Например (взято из документации):
// Create a reference to the SF doc.
var sfDocRef = db.collection("cities").doc("SF");
// Uncomment to initialize the doc.
// sfDocRef.set({ population: 0 });
return db.runTransaction(function(transaction) {
// This code may get re-run multiple times if there are conflicts.
return transaction.get(sfDocRef).then(function(sfDoc) {
if (!sfDoc.exists) {
throw "Document does not exist!";
}
// Add one person to the city population.
// Note: this could be done without a transaction
// by updating the population using FieldValue.increment()
var newPopulation = sfDoc.data().population + 1;
transaction.update(sfDocRef, { population: newPopulation });
});
}).then(function() {
console.log("Transaction successfully committed!");
}).catch(function(error) {
console.log("Transaction failed: ", error);
});
Я хочу заменить переменную sfDocRef на переменную myPromise, но не могу, поскольку одна ссылка на документ, а другая - обещание. Как я могу создать транзакцию для документа, который представляет myPromise?