简体   繁体   English

Firebase Cloud Function:Cloud Firestore 查询无效,即使数据在 Cloud Firestore 中

[英]Firebase Cloud Function : Cloud Firestore query invalid eventhough data is in Cloud Firestore

I have a cloud function that has the following code.我有一个具有以下代码的云 function。 And I call for query from my iOS app.我要求从我的 iOS 应用程序中查询。 Even though the data is in the Cloud Firestore collection, the function still go to the else statement meaning console print "NOT IN COLLECTION".即使数据在 Cloud Firestore 集合中,function 仍然是 go 到 else 语句,意思是控制台打印“NOT IN COLLECTION”。 can someone help?有人可以帮忙吗?

cloud function code:云 function 代码:

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,
  };
});

And this is how I call it from my iOS app:这就是我从我的 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)}
        
    }       
}

If there is no document at the location referenced by docRef , the resulting document will be empty and calling exists on it will return false.如果docRef引用的位置没有文档,则生成的文档将为空,并且在其上调用exists将返回 false。

Aside of double checking the existence of the document you are trying to get, I recommend you to read the example of getting a document from Firestore with Node.js [1].除了仔细检查您尝试获取的文档是否存在之外,我建议您阅读使用 Node.js [1] 从 Firestore 获取文档的示例。

You may solve your issue by using in your code the keyword await [2].您可以通过在代码中使用关键字await [2] 来解决您的问题。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM