ios - 从 firebase 可调用函数接收返回的数据
<p><p>我正在使用 iOS 中的 Callable HTTPS 函数。我已经创建并部署了以下函数:</p>
<pre><code>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')
})
})
</code></pre>
<p>然后我从我的 iOS 应用程序中调用该函数,如下所示:</p>
<pre><code>let callParameters = ["user_id": userId]
self?.functions.httpsCallable("generateLoginToken").call(callParameters) { (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? )?["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))
}
}
}
</code></pre>
<p>我可以在日志中看到在服务器上使用正确的参数调用了该函数,但我似乎无法获取从该函数返回到应用程序的数据。似乎 <code>result.data</code> 值是 <code>nil</code> 出于某种原因,即使我 <code>return {text: "some_data"}</code> 从云函数. <strong>怎么会?</strong></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>哎呀!问题是我忘记从云函数返回实际的 promise 。此功能有效:</p>
<pre><code>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')
})
})
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从 firebase 可调用函数接收返回的数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/50003622/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/50003622/
</a>
</p>
页:
[1]