Node.js iOS到云功能和返回
我正在尝试使用onCall将iOS Swift应用程序与云功能集成。然而,我的简单服务拒绝发送数据回来 以下是我的功能:Node.js iOS到云功能和返回,node.js,swift,google-cloud-functions,Node.js,Swift,Google Cloud Functions,我正在尝试使用onCall将iOS Swift应用程序与云功能集成。然而,我的简单服务拒绝发送数据回来 以下是我的功能: exports.getText = functions.https.onCall((data, context) => { var public_token = data.public_token; if (!(typeof public_token === 'string') || public_token.length === 0) {
exports.getText = functions.https.onCall((data, context) => {
var public_token = data.public_token;
if (!(typeof public_token === 'string') || public_token.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
'one arguments "text" containing the message text to add.');
}
const docRef = admin.firestore().doc(`/PlaidUsers/` + public_token);
docRef.get().then(function(doc) {
if (doc.exists) {
return {"text" : "test"};
} else {
return {"text" : "Document doesn't exist"};
}
}).catch(error => {
return {"text" : "Error getting document"};
});
});
它确实成功地部署到云功能
以下是我的简单Swift代码:
self.functions.httpsCallable("getText").call(["public_token" : self.userMap["plaidPublicToken"]]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
print(message)
}
// ...
}
if let text = (result?.data as? [String: Any])?["text"] as? String {
print (text)
}
}
我得到的错误不仅仅是一个空结果。在JavaScript中,
然后
和捕获
是异步回调方法。您不能从它们中返回要由包含函数返回的数据。您的函数实际上是不向客户机返回任何内容,因为在函数的顶层没有返回语句
then
和catch
都返回另一个承诺,该承诺与回调方法返回的值进行解析。因此,尝试将回报放在承诺链的顶层:
return docRef.get().then(...).catch(...)
仅供参考,您可以通过选择整个代码段并使用编辑器中的{}按钮来格式化整个代码段。谢谢你,你的解释很好。