我决定在将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)
})
})
};