无法使用“句点"更新/删除 Firestore 字段在名字里 [英] Cannot update/delete Firestore field with "period" in the name

查看:16
本文介绍了无法使用“句点"更新/删除 Firestore 字段在名字里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新/删除 Firestore 文档中的字段,但具有句点"的字段将被删除.在尝试更新/删除它们时,名称中似乎默默地失败了.我有句点的原因是我使用 URL 作为对象中的键,我觉得这是一个半常见的用例.

I'm trying to update/delete a fields in a Firestore document, but the fields that have a "period" in the name seem to be silently failing when trying to update/delete them. The reason I have periods is that I'm using URLs as the keys in the object, I feel this is a semi-common use case.

示例:

首先创建文档(这个工作正常)

First create the document (this works fine)

db.collection("data").doc("temp").set({
  helloworld: {
      key1: 'foo'
  },
  hello.world: {
      key1: 'bar'
  }
})

如果您尝试删除没有句点的元素,则效果很好.

If you try to delete the element without the period, it works fine.

db.collection("data").doc("temp").update({
    helloworld: firebase.firestore.FieldValue.delete()
})
// Value is Deleted

如果您尝试删除带有句点的元素,它不会执行任何操作.

If you try to delete the element with the period, it doesn't do anything.

db.collection("data").doc("temp").update({
    hello.world: firebase.firestore.FieldValue.delete()
})
// Nothing Happens!

我也试过

let u = {}
u['hello.world'] = firebase.firestore.FieldValue.delete()
db.collection("data").doc("temp").update(u)
// Nothing Happens!

这是一个错误吗?是否支持字段名称中的句点?我可以创建元素但不能删除它,这似乎很奇怪.

Is this a bug? Are periods in field names supported? It seems strange I can create the element but not delete it.

推荐答案

当您在更新或删除的名称中使用句点时,您需要将其括在引号中,例如:

You need to wrap it in quotes when you use periods in the name on an update or delete like:

db.collection("data").doc("temp").update({
  "hello.world": firebase.firestore.FieldValue.delete()
})

或动态名称:

[`hello.${world}`]: firebase.firestore.FieldValue.delete()

这篇关于无法使用“句点"更新/删除 Firestore 字段在名字里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆