首页 > 解决方案 > 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代码格式错误

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


使固定

我是一个糟糕的 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;
      });
    };

推荐阅读