Abd*_* Ch 6 javascript nosql firebase google-cloud-platform google-cloud-firestore
// Update the other user that current user has asked for revision
export async function updateOtherUserForContractRevision(contractID : string, comments : any) {
// getting userDetails
let currentUser : string | any = localStorage.getItem("userDetails");
currentUser = JSON.parse(currentUser);
// update fields in contractRecieved
const contractsRecievedRef : any = doc(db, "contractsRecieved", currentUser.uid);
// const queryContractsRecieved = query(contractsRecievedRef,
// where('contractDetails.contract.contractID','array-contains',contractID)
// );
// console.log(".////////updateOtherUserForContractRevision", queryContractsRecieved ,contractID)
onSnapshot(contractsRecievedRef, (doc: any) => {
doc.data().contract.map((contract: any) => {
if(contract.contractDetails.contract.contractID === contractID) {
// I reach my desired array Index and want to update the msg field to my comments parametre
console.log(".////////contract", contract);
}
})
})
}
Run Code Online (Sandbox Code Playgroud)
我想更新我的msg字段以及对象content中的字段contract,而对象中的字段又存在于数组contractDetails中的对象中contract(第 0 个索引)。我已使用 搜索并达到了所需的数组值onSnapShot,如何更新onSnapShot方法中的这些字段?或者我应该使用另一种方法来搜索和更新数组包含的对象中的字段。
只是一个想法:如果我可以获得对数组索引的引用,然后使用它来更新对象,也许它会起作用
例如(我会更新sentForRevision)
onSnapshot(contractsRecievedRef, async (doc: any) => {
doc.data().contract.map(async (contract: any) => {
if(contract.contractDetails.contract.contractID === contractID) {
console.log(".////////contract", doc.ref);
// If this contract.ref exists and points to the index present in the document
await updateDoc(contract.ref,{
"contractDetails.sentForRevision": true,
});
}
})
})
Run Code Online (Sandbox Code Playgroud)
您无法根据数组中包含的对象中存在的值查询 Firestore 集合。使用部分数据无法实现这种过滤。我什至写了一篇关于这个主题的文章,名为:
如果需要,可以复制要执行过滤的数据并将其添加到单独的数组中。这样就可以使用array-contains运算符查询集合了。获得所需的文档后,您可以获取数组,执行更新,然后将文档写回 Firestore。
| 归档时间: |
|
| 查看次数: |
1320 次 |
| 最近记录: |