`firebase.firestore.FieldValue.delete()`未删除文档中的字段。

huangapple go评论83阅读模式
英文:

firebase.firestore.FieldValue.delete() not deleting field in document

问题

I'm here to help with the translation:

我正在尝试从React应用程序中使用firebase.firestore.FieldValue.delete()删除Firebase文档中的一个字段,但它不起作用。阅读和写入文档和集合中的数据正常工作,但我无法让FieldValue正常工作。我已经像这样在src/firebase.js中设置了Firebase实例:

import app from 'firebase/app';
import 'firebase/firestore';

var config = {
  ******
};

class Firebase {
  constructor() {
    app.initializeApp(config);
    this.fv = app.firestore.FieldValue;
    this.db = app.firestore();
  };
};

export default Firebase;

然后在我尝试在其中使用FieldValue的模块中,我正在执行以下操作:

import Firebase from "./firebase.js";

class App extends Component {
  constructor(props) {
    super(props);
    this.firebase = new Firebase();
    this.db = this.firebase.db;
    this.fv = this.firebase.fv;
  };

  deleteFunction(id) {
    this.db.collection("collection_id").doc("doc_id").update({
      field_id: this.fv.delete()
    });
  }
};

然而,无论我如何导入或文档引用的变化,都没有成功,就像这里提到的一样。我没有收到任何错误消息,我知道Firestore实例正在正常工作,因为我正在使用它来设置和读取相同的数据。我还验证了我尝试删除的field_id是正确的,以及this.fvFieldValue,通过在控制台中记录它们。

我漏掉了什么?我觉得这应该非常简单和直接,我可能忽略了一些非常明显的东西。

英文:

I'm trying to delete a field in a Firebase document from a React app using firebase.firestore.FieldValue.delete() but it isn't working. Reading and writing data to documents and collections is working fine but I am struggling to get FieldValue to work. I've set up the Firebase instance in src/firebase.js like this:

import app from 'firebase/app';
import 'firebase/firestore';

var config = {
  ******
};

class Firebase {
  constructor() {
    app.initializeApp(config);
    this.fv = app.firestore.FieldValue;
    this.db = app.firestore();
  };
};

export default Firebase;

And then in the module that I am trying to use FieldValue in I am doing:

import Firebase from "./firebase.js";

class App extends Component {
  constructor(props) {
    super(props);
    this.firebase = new Firebase();
    this.db = this.firebase.db;
    this.fv = this.firebase.fv;
  };

  deleteFunction(id) {
    this.db.collection("collection_id").doc("doc_id").update({
      field_id: this.fv.delete()
    });
  }
};

However no variation of imports or doc references as mentioned here have been successful for me. I don't receive any errors and I know that the firestore instance is working correctly because I am using it to set and read the same data. I have also verified that the field_id I am trying to delete is the correct id and that this.fv is FieldValue by console logging them both.

What am I missing? I feel like this should be super easy and straight forward and that I am overlooking something super obvious,

答案1

得分: 6

我的猜测是你正在尝试删除由 field_id 标识的字段,所以说:

// 这个不会起作用
let field_id = "fieldToDelete";
this.db.collection("collection_id").doc("doc_id").update({
  field_id: this.fv.delete()
})

上面的代码尝试删除字面上的 field_id 字段,这个字段可能不存在。

你需要使用 [] 符号来构建传递给 Firestore 的对象。

let field_id = "fieldToDelete";
let updates = {};
updates[field_id] = this.fv.delete()
this.db.collection("collection_id").doc("doc_id").update(updates)

现在你正在使用 field_id来调用 Firestore,它将删除 fieldToDelete

英文:

My guess is that you're trying to delete the field that is identified by field_id, so say:

// THIS WON'T WORK
let field_id = "fieldToDelete";
this.db.collection("collection_id").doc("doc_id").update({
  field_id: this.fv.delete()
})

The above code is trying to delete the literal field_id field, which probably doesn't exist.

You'll need to use [] notation to build the object you pass to Firestore.

let field_id = "fieldToDelete";
let updates = {};
updates[field_id] = this.fv.delete()
this.db.collection("collection_id").doc("doc_id").update(updates)

Now you're using the value of field_id in the call to Firestore, and it will delete fieldToDelete.

huangapple
  • 本文由 发表于 2020年1月6日 22:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定