【发布时间】:2019-06-27 19:08:06
【问题描述】:
首先,我使用 Typescript 编写云函数来创建具有 Firebase 身份验证的帐户。创建新帐户后,尝试向帐户添加自定义声明并将用户信息添加到 Firebase 实时数据库,这些步骤成功完成。
添加现有帐户时出现问题。错误打印在函数日志中,但我无法将其扔给 Android
My firebase cloud functions log
云函数代码:。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { User } from './model/User';
admin.initializeApp();
const db = admin.database();
exports.createSellerAccount = functions.https.onCall((data, context) => {
const userEmail = data.email;
const userPassword = data.password;
const user: User = new User();
const newUserData = JSON.parse(data.newUser);
user.setFirstName(newUserData.firstName);
user.setLastName(newUserData.lastName);
user.setMobileNumber(newUserData.mobileNumber);
admin.auth().createUser({
email: userEmail,
password: userPassword
}).then(function (userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
const additionalClaims = {
sellerAccount: true
};
admin.auth().setCustomUserClaims(userRecord.uid, additionalClaims)
.then(function (customToken) {
// Send token back to client
console.log("Successfully token created new user:", userRecord.uid);
})
.catch((error) => {
console.log("Error creating custom token:", error);
});
db.ref("Users/" + userRecord.uid).set(user)
.then(() => {
console.log("seller info inserted successfully");
})
.catch(function (error) {
console.log("Error while inserting seller info:", error);
});
}).catch(function(error) {
// console.log("Error creating new user:", error);
throw new functions.https.HttpsError('already-exists',error);
});
})
Android 代码:。
private void createAccount() {
ekhtarSeller.showProgressDialog(this);
newUser.setFirstName(tietFirstName.getText().toString().trim());
newUser.setLastName(tietLastName.getText().toString().trim());
newUser.setMobileNumber(tietMobileNumber.getText().toString().trim());
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("email", tietEmail.getText().toString().trim());
data.put("password", tietPassword.getText().toString().trim());
data.put("newUser", new Gson().toJson(newUser));
mFunctions
.getHttpsCallable("createSellerAccount")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
ekhtarSeller.getProgressDialog().cancel();
String result = (String) task.getResult().getData();
Toast.makeText(CreateAccountActivity.this, result, Toast.LENGTH_SHORT).show();
return result;
}
});
}
【问题讨论】:
-
长话短说:您在这段代码中根本没有正确处理承诺。使用可调用函数,您需要在函数的所有异步工作完成后,从函数的顶层返回一个用您想要序列化并发送给客户端的对象解析的承诺。考虑到您根本没有从函数返回承诺,并且您还有其他异步工作的承诺没有被考虑到最终的承诺结果中,您离那还有很长的路要走。
-
@Doug Stevenson 非常感谢,返回承诺后问题已解决。
标签: java android typescript firebase google-cloud-functions