Firestore查询在云功能中不起作用,但在swift中工作

时间:2017-12-22 06:29:06

标签: ios swift firebase google-cloud-functions google-cloud-firestore

我决定在将Firestore实施到我们的项目之前测试它。我在云函数中进行了查询,虽然快速实现相同的查询,但它不起作用。请告诉我node.js查询有什么问题。

更新:如果UserMatch等于传递给函数的参数,则此代码应返回matchedUserID模型。在iOS SDK中这很好用,在Node.js中它不起作用(没有返回任何内容)具有相同的数据。

Swift代码

class func testFirestore(_ matchedUserID: String) {
        let start = Date().currentTimestamp
        let userID = RealmManager().getCurrentUser()?.id ?? ""
        let db = Firestore.firestore().collection("userMatches").document(userID).collection("matches").whereField("matchedUserID", isEqualTo: matchedUserID)

        db.getDocuments { (querySnap, error) in
            if let error = error {
                debugPrint("error", error)
                return
            }
            let end = Date().currentTimestamp
            let result = end - start
            debugPrint("testFirestore result is", result)
            guard let documents = querySnap?.documents else { return }
            debugPrint("documents", documents.count)
            for document in documents {
                debugPrint("document", document.data(), "document.metadata")
            }
        }
    }

来自云功能的Node.js代码。

exports.testFirestore = function (currentUserID, matchedUserID) {
    return new Promise((resolve, reject) => {
        console.log('matchedUserID', matchedUserID);
        const userMatchesRef = firestore.collection('userMatches').doc(currentUserID).collection('matches');
        var query = userMatchesRef.where('matchedUserID', '==', matchedUserID);

        return query.get().then(userMatchSnap => {
            if (userMatchSnap.exists) {
                // should be only on
                var userMatch = null;
                userMatchSnap.forEach(doc => {
                    console.log(doc.id, '=>', doc.data());
                    if (userMatch === null) {
                        userMatch = doc.data();
                    }
                });
                return resolve(userMatch)
            } else {
                return resolve(null);
            }
        }).catch(error => {
            return reject(error)
        })
    })
};

0 个答案:

没有答案