如何解决如何更新Firebase数组中项目的值?
当前,我尝试使用angularfire2更新数组中一项的值。 不幸的是,我只能使用arrayUnion存档将项目添加到数组,并使用arrayRemove将其删除。
这是我的代码:
// here I try to update the value
async createAddress(formValue) {
const arrayUnion = firebase.firestore.FieldValue.arrayUnion;
await this.db.collection('some-collection')
.doc('docId'))
.update({addresses: arrayUnion(formValue)}
);
}
// here I get all items from the array currently stored in
this.addresses = this.afs.collection('collection')
.doc<AppUser>('docId')
.valueChanges()
.pipe(map(doc => {
return doc.addresses;
}));
当我尝试先删除它并再次添加它时,删除操作将被跳过,并且该项目仍在数组中...
arrayUnion = firebase.firestore.FieldValue.arrayUnion;
arrayRemove = firebase.firestore.FieldValue.arrayRemove;
docRef = this.db.collection('some-collection').doc('docId'));
async updateAddress(formValue) {
await this.docRef.update({
addresses: this.arrayRemove(formValue)
}).then( () => {
this.docRef.update({
addresses: this.arrayUnion(formValue)
});
});
}
有人知道如何解决吗?有一个通用的解决方法吗?像在保存之前删除它,以便将新的添加到阵列中?还是复制整个阵列?如果是这样,那么任何人都可以链接到一个示例,那就太好了。谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。