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

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

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实例:

  1. import app from 'firebase/app';
  2. import 'firebase/firestore';
  3. var config = {
  4. ******
  5. };
  6. class Firebase {
  7. constructor() {
  8. app.initializeApp(config);
  9. this.fv = app.firestore.FieldValue;
  10. this.db = app.firestore();
  11. };
  12. };
  13. export default Firebase;

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

  1. import Firebase from "./firebase.js";
  2. class App extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.firebase = new Firebase();
  6. this.db = this.firebase.db;
  7. this.fv = this.firebase.fv;
  8. };
  9. deleteFunction(id) {
  10. this.db.collection("collection_id").doc("doc_id").update({
  11. field_id: this.fv.delete()
  12. });
  13. }
  14. };

然而,无论我如何导入或文档引用的变化,都没有成功,就像这里提到的一样。我没有收到任何错误消息,我知道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:

  1. import app from 'firebase/app';
  2. import 'firebase/firestore';
  3. var config = {
  4. ******
  5. };
  6. class Firebase {
  7. constructor() {
  8. app.initializeApp(config);
  9. this.fv = app.firestore.FieldValue;
  10. this.db = app.firestore();
  11. };
  12. };
  13. export default Firebase;

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

  1. import Firebase from "./firebase.js";
  2. class App extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.firebase = new Firebase();
  6. this.db = this.firebase.db;
  7. this.fv = this.firebase.fv;
  8. };
  9. deleteFunction(id) {
  10. this.db.collection("collection_id").doc("doc_id").update({
  11. field_id: this.fv.delete()
  12. });
  13. }
  14. };

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 标识的字段,所以说:

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

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

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

  1. let field_id = "fieldToDelete";
  2. let updates = {};
  3. updates[field_id] = this.fv.delete()
  4. 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:

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

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.

  1. let field_id = "fieldToDelete";
  2. let updates = {};
  3. updates[field_id] = this.fv.delete()
  4. 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:

确定