【问题标题】:Firebase cloud function + iOS app : return true/false to client app from callable cloud functionFirebase 云功能 + iOS 应用程序:从可调用的云功能向客户端应用程序返回 true/false
【发布时间】:2021-06-20 00:55:42
【问题描述】:

我有一个云功能,可以在从客户端应用程序调用它时更新用户输入。我希望该函数返回 true/false 以在客户端应用程序中执行进一步操作。例如,如果云函数返回 true,则新屏幕切换。否则,显示警报。但问题是,如何修改我的代码以从云函数返回真/假并在 iOS 应用程序中读取它?有人可以帮忙吗?

这是我的云函数代码(如果从这里成功返回true):

exports.updateUserDetails = functions.https.onCall((data, context) => {
  const uid = context.auth.uid;
  console.log("UID: " + uid);

  const phoneNo = data.phoneNo;
  console.log("Recieved: "+ phoneNo);

  const docRef = db.collection("Users").doc(uid);
  const code = docRef.set({
    "phoneNo": numberID,
    "dateUpdated": timeStamp,
  }, {merge: true});
  console.log("Phone Number updated");

  return {
    message: phoneNo,
  };
});

这是来自 iOS 应用程序的代码(如何读取结果以进行进一步操作);

func updateUser(){
    let data = ["phoneNo":"0125949022"]
    
    functions.httpsCallable("updateUserDetails").call(data) { (result, error) in
        print("Function returned")
        if let err = error {print(err)}
        if let res = result {print(res)}
    }
}

【问题讨论】:

  • 所以,基本上你想要的是返回 true/false 而不是 phoneNo?这对您当前的逻辑没有意义,请详细说明此布尔返回的条件是什么。从我的角度来看,你应该考虑为此创建一个单独的函数,因为我相信你会对这个函数有不同的逻辑。
  • 如果更新成功我想返回,返回{success: true}给客户端应用

标签: javascript node.js swift google-cloud-firestore google-cloud-functions


【解决方案1】:

您所要做的就是创建一个then(),它将等待异步 Firestore 操作完成,因此您可以将其应用到您的代码中:

const docRef = db.collection("Users").doc(uid);
docRef.set({
    "phoneNo": numberID,
    "dateUpdated": timeStamp,
}, {merge: true})
.then((docRef) => {
    console.log("Phone Number updated");
    return {success: true};
})
.catch((error) => {
    console.error("Error adding document: ", error);
    return {success: false};
});

【讨论】:

  • 您好,来自客户端应用程序如何读取成功结果?
猜你喜欢
  • 2018-09-06
  • 2021-01-17
  • 2018-09-04
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2020-12-24
相关资源
最近更新 更多