ios - 从 firebase 可调用函数接收返回的数据
问题描述
我正在使用 iOS 中的 Callable HTTPS 函数。我创建并部署了以下功能:
export const generateLoginToken = functions.https.onCall((data, context) => {
const uid = data.user_id
if (!(typeof uid === 'string') || uid.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with one argument "user_id" ');
}
admin.auth().createCustomToken(uid)
.then((token) => {
console.log("Did create custom token:", token)
return { text: "some_data" };
}).catch((error) => {
console.log("Error creating custom token:", error)
throw new functions.https.HttpsError('internal', 'createCustomToken(uid) has failed for some reason')
})
})
然后我从我的 iOS 应用程序中调用该函数,如下所示:
let callParameters = ["user_id": userId]
self?.functions.httpsCallable("generateLoginToken").call(callParameters) { [weak self] (result, error) in
if let localError = self?.makeCallableFunctionError(error) {
single(SingleEvent.error(localError))
} else {
print("Result", result)
print("data", result?.data)
if let text = (result?.data as? [String: Any])?["text"] as? String {
single(SingleEvent.success(text))
} else {
let error = NSError.init(domain: "CallableFunctionError", code: 3, userInfo: ["info": "didn't find custom access token in the returned result"])
single(SingleEvent.error(error))
}
}
}
我可以在日志中看到使用正确参数在服务器上调用了该函数,但我似乎无法获取从该函数返回到应用程序的数据。似乎该result.data
值是nil
出于某种原因,即使我return {text: "some_data"}
来自云功能。怎么来的?
解决方案
哎呀!问题是我忘记从云函数返回实际的承诺。此功能有效:
export const generateLoginToken = functions.https.onCall((data, context) => {
const uid = data.user_id
if (!(typeof uid === 'string') || uid.length === 0) {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with one argument "user_id" ');
}
return admin.auth().createCustomToken(uid)
.then((token) => {
console.log("Did create custom token:", token)
return { text: "some_data" };
}).catch((error) => {
console.log("Error creating custom token:", error)
throw new functions.https.HttpsError('internal', 'createCustomToken(uid) has failed for some reason')
})
})
推荐阅读
- sql - Rspec 在执行自定义 SQL“ALTER TABLE”语句时截断 schema_migration 表
- reactjs - 我们如何使用 react-spring 动画显示记录
- c++ - golang os.Setenv 在 cgo C.dlopen 中不起作用?
- microsoft-graph-api - 如何使用 Graph API 读取 sharepoint 在线列表的附件
- html - 拖放后如何保存卡片的位置
- flutter - Flutter ListWheelScrollView.useDelegate 使自动滚动几秒钟
- ios - 如何使用swift从本地JSON文件中解析两个数组
- flutter - Flutter - 在纵向和横向模式之间转换滚动位置
- azure-ad-b2c - 登录成功后的回调事件 msal-react
- javascript - 循环 document.getElementsByClassName