Firebase Cloud函数:尽管数据在Cloud Firestore中,但Cloud Firestore查询无效事



我有一个云函数,它有以下代码。我打电话询问我的iOS应用程序。即使数据在Cloud Firestore集合中,函数仍然转到else语句,这意味着控制台打印";不在收集中";。有人能帮忙吗?

云功能代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
const FieldValue = admin.firestore.FieldValue;
exports.validateShop = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
console.log("Function called by UID: " + uid);
const email = context.auth.token.email;
console.log("Email: " + email);
const shop = data.shop;
console.log("Recieved: "+ data);
const docRef = db.collection("Users").where("shopName", "==", shop);
docRef.get().then(function(doc) {
if (doc.exists) {
const docDelete = db.collection("shops").doc(uid);
const updateDoc = docDelete.doc(uid).update({
"shopName": FieldValue.delete(),
});
console.log(shop + ": EXISTS. DOCUMENT UPDATED ");
return {success: true};
} else {
console.log(shop + ": NOT IN COLLECTION ");
return {success: false};
}
}).catch((error) => {
return {"shop": "Error getting document"};
});
return {
message: shop,
};
});

这就是我在iOS应用程序中的称呼:

func validateTurn(){
let data = ["shop": "ThaiSook"]

functions.httpsCallable("validateShop").call(data) { (result, error) in
print("Function returned")
if let err = error {print(err)}
if let res = result {print(res)}

}       
}

如果docRef引用的位置没有文档,则生成的文档将为空,对其调用exists将返回false。

除了仔细检查您试图获取的文档的存在性之外,我建议您阅读使用Node.js[1]从Firestore获取文档的示例。

您可以通过在代码中使用关键字await[2]来解决问题。

[1]https://firebase.google.com/docs/firestore/query-data/get-data#get_a_document

[2]https://javascript.info/async-await

最新更新