简体   繁体   English

Firestore:如何更新有效负载文档中的特定字段?

[英]Firestore: How to update specific field In payload document?

How do I access and update a specific field in angular firebase but My Data In payload I have put the image link that easy for u:如何访问和更新 angular firebase 中的特定字段,但我的数据在有效负载中我已将图像链接放在对您来说很容易的位置:

Enter This Link and see the Image what I am saying that输入此链接并查看图像我在说什么

 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);
     });
    }

if I use this code then update the data outside the payload but I want to update the data inside the payload?如果我使用此代码然后更新有效负载之外的数据,但我想更新有效负载内的数据?

Your code yes will update the data outside payload, because you didn't specify that.您的代码 yes 将更新有效负载之外的数据,因为您没有指定。 The right way to update a field inside an object in a document (called nested objects):更新文档中 object 中的字段的正确方法(称为嵌套对象):

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

You can find the whole documentation here: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects您可以在此处找到整个文档: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

But this won't work for you , because your payload type is not a map (Object) and that is obvious from the double quote, which mean its a string(make sure when you set it in the right way).但这对您不起作用,因为您的有效负载类型不是 map(对象),这从双引号中很明显,这意味着它是一个字符串(确保以正确的方式设置它)。
However, if you still want it this way, then you have to update the whole payload string, including other values(also you could do this if the payload is an object, like before):但是,如果您仍然想要这种方式,那么您必须更新整个有效负载字符串,包括其他值(如果有效负载是 object,您也可以这样做,就像以前一样):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM