Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 删除Firestore文档中的字段_Angular_Typescript_Firebase_Ionic3_Google Cloud Firestore - Fatal编程技术网

Angular 删除Firestore文档中的字段

Angular 删除Firestore文档中的字段,angular,typescript,firebase,ionic3,google-cloud-firestore,Angular,Typescript,Firebase,Ionic3,Google Cloud Firestore,如何删除云Firestore中的文档字段。。。我正在使用下面的代码,但我不能 this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({ [currentUserId]: firebase.firestore.FieldValue.delete()}) 有人知道怎么做吗?您可以按如下所示进行尝试: // get the reference to the doc let docRef=this.db.d

如何删除云Firestore中的文档字段。。。我正在使用下面的代码,但我不能

this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`).update({ 
[currentUserId]: firebase.firestore.FieldValue.delete()})

有人知道怎么做吗?

您可以按如下所示进行尝试:

// get the reference to the doc
let docRef=this.db.doc(`ProfileUser/${userId}/followersCount/FollowersCount`);

// remove the {currentUserId} field from the document
let removeCurrentUserId = docRef.update({
    [currentUserId]: firebase.firestore.FieldValue.delete()
});

我想到的另一个解决方案是,与其专注于更新文档,还不如删除字段,更新整个文档。例如:

let数据:部分={
//这里有些田地,
那个领域:'',
//那里有些田地
};
此.angularFirestore.collection('collection_name').doc('id').set(数据);
此外,您可能不包括该字段,而不是初始化该字段


出于某种原因,所选答案(
firebase.firestore.FieldValue.delete()
)对我不起作用但确实如此:

只需将该字段设置为
null
,它就会被删除

//获取对文档的引用
让docRef=this.db.doc(`ProfileUser/${userId}/followersCount/followersCount`);
//从文档中删除{currentUserId}字段
让removeCurrentUserId=docRef.update({
[currentUserId]:空
});
这对我很有效。(还用于删除具有空值的字段)


谢谢你的回复。我在代码中尝试了这个更改,但仍然没有删除字段,我做了一个测试,我可以使用类似的代码更新布尔值,但是删除函数没有在字段中进行任何更改。请参阅文档。它应该可以工作吗?您的
文档参考资料如何?缺少
var docRef=firebase.firestore()
在构造函数中,我将您的代码更改为
var docRef=this.db.doc(
ProfileUser/${userId}/followerscont/followerscont
);让removeCurrentUserId=docRef.update({[currentUserId]:firebase.firestore.FieldValue.delete()})现在工作得很好,谢谢!!!很高兴听到它的帮助:)
firebase.firestore.FieldValue.delete()
对于一个已经为
null
的字段不起作用,你说的那部分对你不起作用,对我起作用!谢谢。将值设置为
null
将在Firestore.Hmm中将值保存为
null
,我刚刚测试了它,如果设置为
null
,它会删除该值!我感觉您正在将其设置为字符串形式的“null”
PS:从技术上讲,它们永远不会在数据库中存储
null
undefined
(当然这些字符串值不再是null或undefined!),即使是空字符串(或无值)也无法存储。
document.ref.update({
  FieldToDelete: admin.firestore.FieldValue.delete()
})