Angular 删除Firestore文档中的字段
如何删除云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
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()
})