【问题标题】:geoFirestore - add new document with array as one of the document data propertygeoFirestore - 添加带有数组的新文档作为文档数据属性之一
【发布时间】:2020-04-27 17:24:04
【问题描述】:

当我尝试使用 geoFirestore 将新文档添加到数据库并尝试使用 DocumentReference 将属性之一设置为数组(指 Firestore 数据库中的文档位置)时,我收到以下错误:

FirebaseError:使用无效数据调用函数 DocumentReference.set()。不支持的字段值:自定义对象

如果我在那里设置一个空数组,它就成功了。 顺便说一句,如果我在那里设置一个字符串或数字它的工作也

插入代码:

await geocollection.add({
                        coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
                        arrayOfRef: [newRef]
                     });

我尝试将Object.asset() 用于newRef,但得到相同的结果。

感谢帮助。

【问题讨论】:

    标签: database typescript firebase geofirestore


    【解决方案1】:

    所以在很多情况下geofirestore 会最低限度地修改您提供的文档,然后将其传递给 Firestore。在这种情况下,看起来 Firebase/Firestore 正在抛出错误,而不是 geofirestore

    我怀疑问题出在您使用的参考文献上。如果该引用由geofirestore 提供,则Firestore 使用的不一定是DocumentReference,而是GeoDocumentReference。您可能希望从native property 获得实际的DocumentReference

    await geocollection.add({
      coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng),
      arrayOfRef: [newRef.native]
    });
    

    如果有帮助,请告诉我!我还对您的代码做了一些假设,因此在您调用 add 方法之前看到更多发生的事情会很棒。

    【讨论】:

    • 嗨 michael,谢谢,但不是这种情况,我将 newRef 设置为 DocumentReference。我看到了使用 Object.assign() 的建议,但仍然不起作用。
    • 我可以查看更多代码吗,比如geocollection.add 之前的所有内容?
    • 如你所见,newRef 是从 insertNewRef 返回的,它使用 firestore-admin 插入新文档并返回它的 DocumentReferenceawait GeoLocationDB.get().then(async function(querySnapshot){ await insertNewRef().then(async function(newRef){ if (querySnapshot.size == 0){ await geocollection.add({ coordinates: new firebase.firestore.GeoPoint(latlng.lat, latlng.lng), posts: [newRef] });
    • 好的,所以我尝试在 StackBlitz 中实现你正在做的事情(我使用的是 Web 客户端,而不是服务器)。这是闪电战,它似乎工作得很好。但是我注意到 Firestore 的一个问题,如果坐标为 [0, 0] 则会引发问题(我将把这个问题传递给 Firebase 团队)stackblitz.com/edit/geofirestore-169?file=index.ts
    猜你喜欢
    • 2019-07-29
    • 2019-01-29
    • 2019-09-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多