javascript - React Native Firestore 删除数组中的对象
问题描述
我无法从 Firestore 的文档中删除我的对象数组中的对象。
我试过了
const obj = product;
const user_post = await this.checkOwnerPost();
console.log(obj);
if (user_post){
await this.firestore
.collection('testCollection')
.doc(user_post.doc_id)
.update({
products: firebase.firestore.FieldValue.arrayRemove(obj)
.then(() => {
console.log("Document successfully updated!");
return true;
}).catch((error) => {
console.log(error);
return false;
})
});
}
Console.log(obj) = {
"description": "1",
"key": 1,
"name": "first",
"picture": "https://firebasestorage.googleapis.com/v0/b/url",
"price": 1,
}
这里的错误消息:
[Unhandled promise rejection: TypeError: undefined is not a function (near '..._firebase.default.firestore.FieldValue.arrayRemove(obj).then...')]
at node_modules/lodash/lodash.js:10487:35 in debounced
at [native code]:null in flushedQueue
at [native code]:null in callFunctionReturnFlushedQueue
有人可以帮助我吗?或者您有什么想法可以删除和更新数组中的对象吗?
编辑:修复,JS代码格式错误
解决方案
使固定
我是一个糟糕的 JS 代码格式。
工作代码是:
if (user_post){
await this.firestore
.collection('testCollection')
.doc(user_post.doc_id)
.update({products: firebase.firestore.FieldValue.arrayRemove(obj)})
.then(() => {
console.log("Document successfully updated!");
return true;
}).catch((error) => {
console.log(error);
return false;
});
};
推荐阅读
- events - Cakephp 3.x Table Beforefind,无法停止事件
- javascript - 如何将值插入嵌套数组
- android - Android中的循环控制
- powershell - 在 ForEach 循环中被覆盖的变量 - Powershell
- webrtc - 使用 webrtc 进行音频广播
- c# - 升级 NuGet 包后出现 Phantom 编译错误
- c++ - 如何编辑 GrammarFst 的权重?
- c# - 从 WebAPI 在浏览器中播放 WAV 文件
- c# - CKEditor 4 和 ASP.NET 核心 MVC:如何在 ckeditor 中更改图像 url
- c - 我正在尝试将文件中的字符串列表存储到 C 中的数组中