我正在嘗試記錄對客戶記錄集合的更改。為了保持無懈可擊,日志記錄顯然應該在 Firestore 事務中創建。我在應用客戶檔案更改時沒有問題transaction.set,但每次此類更改都需要在我的transactionLogs集合中創建一個新檔案。在這里,事情發生了嚴重錯誤日志檔案由時間戳欄位標識,當我運行以下代碼時:...
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);
});
}
...transaction.set指令失敗了,比如"Invalid collection reference. Collection references must have an odd number of segments, but transactionsLogs/1645451217221 has 2."在這個特定的例子中,1645451217221 將是 transactionLog.timesStamp 的值。
有人對這里發生的事情以及如何解決錯誤有任何建議嗎?我的理解是,transaction.set如果提供的參考不存在,它將創建一個新記錄,所以我應該在正確的路線上。但是為什么 Firestore 認為我想在一個名為 transactionsLogs/1645451217221 的集合中創建它?如何在名為“transactionsLogs”的集合中為由字串“1645451217221”標識的檔案創建參考?
uj5u.com熱心網友回復:
如果您要指定檔案 ID(而不是使用自動生成的 ID),那么您必須使用doc()而不是collection()創建DocumentReference:
const newLogRef = doc(db, 'transactionLogs', transactionLog.timeStamp);
該collection()函式用于創建CollectionReference。
另請查看:Firestore:在 Web v9 中添加新資料的模式是什么?
我的理解是,
transaction.set如果提供的參考不存在,它將創建一個新記錄
如果檔案存在,它將覆寫現有檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/430094.html
標籤:javascript 火力基地 谷歌云火库 交易
