【问题标题】:Firestore: How to update specific field In payload document?Firestore:如何更新有效负载文档中的特定字段?
【发布时间】:2021-12-06 05:18:43
【问题描述】:

如何访问和更新 Angular Firebase 中的特定字段,但我的数据在有效负载中我已将图像链接放在对你来说很容易:

[输入此链接并查看图片我在说什么] 1

 UserVerification(id, _value: number) {


      
     console.log(id,"KKKKK",_value)
    //return this.firestore.collection("DevicesData").doc(id).update({Impact:"100 "}); 
      return this.firestore
     .collection("DevicesData")
     .doc('/' + '00HcetBoNFIOmwEWZ6fo')
    //  .doc(id)
     .update({Impact:"_value"})
     .then(() => {
       console.log('done');
     })
     .catch(function(error) {
      console.error('Error writing document: ', error);
     });
    }

如果我使用此代码,则更新有效负载外的数据,但我想更新有效负载内的数据?

变化

1.Add Document in Collection

Now In this I'll show how can i put the data .first column I write the payload then second column I put string then after last column I put the my data I'll give a data for You soo please put this data in your payload no
{"Topic":"IMPACT","SID":"1","GPSlat":"634312957","GPSlong":"104014716","GPSalt":"49710","GPSspd":"63","GPShead":"29499222","GPSepoch":"1638504105","Impact":"20200"}

2.Second Image you check please

Now In second image I'll show what is the data sequence my data is in one line first and your data is after payload :
Topic:"Impact"
impact:"2000"
etc.
after your sequence of data is in one line (in this second image I clearly what the data sequence soo please check the image clearly) after that you try to put the update query to update the data then I'll show my error in third image 

3.This is my error image

Now you understand what I am saying that ? Or Not

【问题讨论】:

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您的代码 yes 将更新有效负载之外的数据,因为您没有指定。更新文档中对象(称为嵌套对象)内的字段的正确方法:

return this.firestore
           .collection("DevicesData")
           .doc('00HcetBoNFIOmwEWZ6fo')
           .update({'payload.Impact':_value});

您可以在此处找到整个文档:https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

但这对你不起作用,因为你的有效载荷类型不是地图(对象),这从双引号中很明显,这意味着它是一个字符串(确保当你设置以正确的方式)。
但是,如果您仍然想要这种方式,那么您必须更新整个有效负载字符串,包括其他值(如果有效负载是一个对象,您也可以这样做,就像以前一样):

return this.firestore
           .collection("DevicesData")
           .doc('00HcetBoNFIOmwEWZ6fo')
           .update({payload:{Topic:'IMPACT', MAC:'...', ..., Impact:_value});

更新
我确实尝试了如下结构和代码,这是正确的做法:
这是之前的数据:

然后我运行这段代码:

await firebaseFirestore
        .collection('workflow')
        .doc('jLcWTZBa5Tfjdex5fiYk')
        .update({ 'payload.Impact': 2000 })
        .then(_ => console.info('Success'))
        .catch(error => {
            console.error(error);
        });

我得到Success,数据变成这样:

除此之外我真的不知道你的问题是什么。

【讨论】:

  • 当我尝试第一个代码时,在 firebase 中只显示一个数据,即有效载荷 Impact:5555
  • 但是当我尝试第二个代码然后显示错误“在 JSON.parse 中转换此代码”后显示此
  • 第一个代码累了,payload的类型是不是变成了map?如果是,那么正如我告诉您的那样,您的有效负载是字符串而不是映射,那么它将覆盖它。从第一个控制台(不是代码)尝试向有效负载添加另一个字段,然后再次使用第一个代码,这应该只更新“影响”。
  • 我尝试以不同的方式使用很多时间,但是当我更新以下数据时,此数据将替换所有有效负载数据并放入新的有效负载数据,我正在使用此 .update({payload:"{'Impact ':'_value'}"}) soo 请问现在是什么错误
  • 尝试帮助我我尝试了很多次所以现在你可以给我发送完美的代码吗
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多