Javascript 可以使用cloud funct更新firestore数据库中以下数据结构中的字段
我有一个对象结构形式的数据结构。[![在此处输入图像描述][1][1] 我有一个条件,当其邀请(1,2,3)电话号码与其他文档匹配时,被邀请者需要将字段更新为Status=trueJavascript 可以使用cloud funct更新firestore数据库中以下数据结构中的字段,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个对象结构形式的数据结构。[![在此处输入图像描述][1][1] 我有一个条件,当其邀请(1,2,3)电话号码与其他文档匹配时,被邀请者需要将字段更新为Status=true 当我尝试将字段更新为Status=true时,它将在文档末尾进行更新。 Mycode需要更新 var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentI
当我尝试将字段更新为Status=true时,它将在文档末尾进行更新。 Mycode需要更新
var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph + "" + status + " to pay $" + document.Amount;
var fulldoc = dbref.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document');
} else {
console.log('Document data :', doc.data());
d1 = doc.data();
console.log("d1 is" + d1);
for (var k in d1) {
var p = d1[k].PhoneNumber;
console.log("ivitees phone" + p);
if (receiverph == p) // Here the condition is true of the invite phoneNumber then need to update
{
console.log("p" + PhoneNumber);
console.log("the phonenumber matches");
var updated=dbref.update({“Status”:Status});//这里正在更新
在文件末尾
要更新的其他方法
d1.状态=状态;//在d1中,我有文档数据
var setdata=dbref.set(d1)
如果他们有任何方法,请帮助我。
谢谢如果我正确理解您希望更新文档中的每个InviteXX项,以下是一个有效的代码(我保留了代码的主要部分): 请注意:
fulldoc
。(你可以看一下这个视频)请分享您已经编写的代码以及遇到的问题/错误。此外,字段状态是否与3个InviteX字段在同一文档中?如果我的条件为真,我需要更新任何invite中的状态。K我将分享我的代码好的,谢谢你的代码。当您说“在文档末尾更新”不是您要查找的内容时,这意味着您要更新文档中的每个Invite字段。是这样吗?@vijju那么你的支票怎么样了?如果(d1.hasOwnProperty(key))它现在做什么line@vijju看见我想说的是,在这种特殊情况下,这不是绝对强制性的,而是一种应用的最佳实践。@vijju你能在这个问题上取得进展吗?嘿,我找到了解决方案
var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph + "" + status + " to pay $" + document.Amount;
var fulldoc = dbref.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document');
} else {
console.log('Document data :', doc.data());
d1 = doc.data();
console.log("d1 is" + d1);
for (var k in d1) {
var p = d1[k].PhoneNumber;
console.log("ivitees phone" + p);
if (receiverph == p) // Here the condition is true of the invite phoneNumber then need to update
{
console.log("p" + PhoneNumber);
console.log("the phonenumber matches");
var dbref = db1.collection('deyaPayUsers').doc(sendauthid).collection('Split').doc(sendauthid).collection('SentInvitations').doc(senderautoid);
var msg1 = receiverph +"" + status +" to pay $"+document.Amount;
const fulldoc = dbref.get()
.then(doc => {
if (doc.exists) {
//console.log("Document data:", doc.data());
const inviteUpdate = {}; //An object that we will update by looping over the InviteXX objects of the document
const d1 = doc.data();
//console.log("d1 is" + d1);
for (let key in d1) {
if (d1.hasOwnProperty(key)) {
const p = d1[key].PhoneNumber;
//console.log(key);
//console.log("invitees phone " + p);
if (receiverph === p) // Here the condition is true of the invite phoneNumber then need to update
{
inviteUpdate[key + '.status'] = true;
//The key point is here: we define the object field with a mix of dot notation and []
}
}
}
return dbref.update(inviteUpdate);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
throw "No such document"
}
})
.catch(function (error) {
console.log("Error:", error);
});