Firebase / Firestore:不存在文档,但应该

时间:2019-02-07 16:34:13

标签: javascript firebase cordova google-cloud-firestore

对于来自firestore调用的控制台输出,我得到No such document!,即使那里有一个文档(我可以在firebase控制台中看到它)。以下是显示我的firestore数据库的屏幕截图:

enter image description here enter image description here enter image description here

这是我的代码:

    var db = firebase.firestore();

    // Disable deprecated features
    db.settings({
      timestampsInSnapshots: true
    });

    console.log("app.selectedPoolId:", app.selectedPoolId);

    db.collection("pools").doc(app.selectedPoolId).collection("items").doc("items_array").get()
        .then(function(doc) {
            if (doc.exists) {
                ...
            } else {
                // doc.data() will be undefined in this case
                console.log("No such document!");
            }
        })
        .catch(function(error) {
            console.error("Error writing document: ", error);
        });

控制台输出

app.selectedPoolId: Eamon's pool

当我尝试在设备上从ios/phonegap运行我的混合XCode应用时,会发生这种情况。我想念什么?

更新

我输出了doc对象的键和值,以防万一有人发现任何奇怪的东西。

此代码:

for (var prop in doc) {
     console.log("doc key:", prop);
     console.log("doc value:", doc[prop]);
}

产生此输出:

2019-02-07 16:05:21.306952-0500 HybridPool[5063:1305478] app.selectedPoolId: Eamon's pool
2019-02-07 16:05:21.524750-0500 HybridPool[5063:1305478] doc key: _firestore
2019-02-07 16:05:21.524868-0500 HybridPool[5063:1305478] doc value: [object Object]
2019-02-07 16:05:21.524987-0500 HybridPool[5063:1305478] doc key: _key
2019-02-07 16:05:21.525044-0500 HybridPool[5063:1305478] doc value: pools/Eamon's pool/items/items_array
2019-02-07 16:05:21.525095-0500 HybridPool[5063:1305478] doc key: _document
2019-02-07 16:05:21.525133-0500 HybridPool[5063:1305478] doc value:
2019-02-07 16:05:21.525171-0500 HybridPool[5063:1305478] doc key: _fromCache
2019-02-07 16:05:21.525208-0500 HybridPool[5063:1305478] doc value: false
2019-02-07 16:05:21.525274-0500 HybridPool[5063:1305478] doc key: _hasPendingWrites
2019-02-07 16:05:21.525388-0500 HybridPool[5063:1305478] doc value: false
2019-02-07 16:05:21.525435-0500 HybridPool[5063:1305478] doc key: data
2019-02-07 16:05:21.525512-0500 HybridPool[5063:1305478] doc value: function (t){return mr("DocumentSnapshot.data",arguments,0,1),t=lc("DocumentSnapshot.data",t),this._document?this.convertObject(this._document.data,ri.fromSnapshotOptions(t,this._firestore._areTimestampsInSnapshotsEnabled())):void 0}
2019-02-07 16:05:21.525561-0500 HybridPool[5063:1305478] doc key: get
2019-02-07 16:05:21.525624-0500 HybridPool[5063:1305478] doc value: function (t,e){if(mr("DocumentSnapshot.get",arguments,1,2),e=lc("DocumentSnapshot.get",e),this._document){var n=this._document.data.field(ju("DocumentSnapshot.get",t));if(void 0!==n)return this.convertValue(n,ri.fromSnapshotOptions(e,this._firestore._areTimestampsInSnapshotsEnabled()))}}
2019-02-07 16:05:21.525672-0500 HybridPool[5063:1305478] doc key: id
2019-02-07 16:05:21.525711-0500 HybridPool[5063:1305478] doc value: items_array
2019-02-07 16:05:21.525749-0500 HybridPool[5063:1305478] doc key: ref
2019-02-07 16:05:21.525818-0500 HybridPool[5063:1305478] doc value: [object Object]
2019-02-07 16:05:21.525857-0500 HybridPool[5063:1305478] doc key: exists
2019-02-07 16:05:21.525895-0500 HybridPool[5063:1305478] doc value: false
2019-02-07 16:05:21.525933-0500 HybridPool[5063:1305478] doc key: metadata
2019-02-07 16:05:21.525971-0500 HybridPool[5063:1305478] doc value: [object Object]
2019-02-07 16:05:21.526008-0500 HybridPool[5063:1305478] doc key: isEqual
2019-02-07 16:05:21.526232-0500 HybridPool[5063:1305478] doc value: function (e){if(!(e instanceof t))throw Nr("isEqual","DocumentSnapshot",1,e);return this._firestore===e._firestore&&this._fromCache===e._fromCache&&this._key.isEqual(e._key)&&(null===this._document?null===e._document:this._document.isEqual(e._document))}
2019-02-07 16:05:21.526289-0500 HybridPool[5063:1305478] doc key: convertObject
2019-02-07 16:05:21.526329-0500 HybridPool[5063:1305478] doc value: function (t,e){var n=this,r={};return t.forEach(function(t,i){r[t]=n.convertValue(i,e)}),r}
2019-02-07 16:05:21.526429-0500 HybridPool[5063:1305478] doc key: convertValue
2019-02-07 16:05:21.526496-0500 HybridPool[5063:1305478] doc value: function (t,e){if(t instanceof gi)return this.convertObject(t,e);if(t instanceof vi)return this.convertArray(t,e);if(t instanceof mi){var n=t.value(e),r=this._firestore.ensureClientConfigured().databaseId();return t.databaseId.isEqual(r)||$n("Document "+this._key.path+" contains a document reference within a different database ("+t.databaseId.projectId+"/"+t.databaseId.database+") which is not supported. It will be treated as a reference in the current database ("+r.projectId+"/"+r.database+") instead."),new rc(n,this._firestore)}return t.value(e)}
2019-02-07 16:05:21.526629-0500 HybridPool[5063:1305478] doc key: convertArray
2019-02-07 16:05:21.526713-0500 HybridPool[5063:1305478] doc value: function (t,e){var n=this;return t.internalValue.map(function(t){return n.convertValue(t,e)})}

更新

我意识到我可以在where上使用collection来获得相同的文档,但是当我尝试这样做时-好像该函数从未执行一样-没有错误消息或任何其他类型的消息-在以下时间停止:

console.log("app.selectedPoolId:", app.selectedPoolId);

由于上面的行,它输出相同的内容,然后停止运行而没有任何错误,这是我正在运行的查询集合的代码:

    db.collection("pools").where("name", "==", app.selectedPoolId).get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
            });
        })
        .catch(function(error) {
            console.error("Error writing document: ", error);
        });

同样,以上代码没有输出结果。

以下是firestore的最佳做法-我仔细研究了一下,并认为我的代码符合:

Firestore Best Practices

1 个答案:

答案 0 :(得分:1)

对此我不确定,但是问题可能出在文档名称“ Eamon's pool”中有一个空格,因此它无法识别全部内容,或者停在字符串“ Eamon's”的第一部分。

过去,当我使用Firestore时,我使用的文档名称不包含空格,因此有空格可能是问题。

尝试将文档重命名为EamonsPool或没有空格的名称,然后重试。