Typescript 尽管一切正常,但从firebase云获取null
我已经用TypeScript编写了一个云函数,其中包含Typescript 尽管一切正常,但从firebase云获取null,typescript,firebase,google-cloud-functions,Typescript,Firebase,Google Cloud Functions,我已经用TypeScript编写了一个云函数,其中包含async调用 exports.validateOtp = functions.https.onCall((data,context)=>{ phoneNumber = data.phoneNumber; otp = data.otp; let email:string = data.email; let password:string = data.password; let displayName:string=
async
调用
exports.validateOtp = functions.https.onCall((data,context)=>{
phoneNumber = data.phoneNumber;
otp = data.otp;
let email:string = data.email;
let password:string = data.password;
let displayName:string= data.displayName;
authFunction.otpValidation(phoneNumber,otp,(otpErr,otpValue) => {
if(otpErr){
console.error(otpErr);
return {otpErr};
}else{
return authFunction.createUser(email,false,phoneNumber,password,displayName,false,(err,value) => {
if(err)
{
console.error(err);
return Promise.reject(err);
}
else{
console.log(value);
return Promise.resolve(value);
}
});
}
});
});
下面是authFunction.otpValidation
otpValidation(phoneNumber:string, otp:string,callback:Function){
let otpValidationApi:string = "https://<api>/verifyRequestOTP.php?authkey="+this.authKey+"&mobile="+phoneNumber+
"&otp="+otp;
https.get(otpValidationApi, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
let result = JSON.parse(data);
var y=result.type;
callback(null,y);
});
}).on("error",(err)=>{
console.log("Error: "+err.message);
});
}
otpValidation(phoneNumber:string,otp:string,callback:Function){
让otpValidationApi:string=”https:///verifyRequestOTP.php?authkey=“+this.authKey+”&mobile=“+phoneNumber”+
“&otp=“+otp;
https.get(otpValidationApi,(resp)=>{
让数据=“”;
在('数据',(块)=>{
数据+=块;
});
分别在('结束',()=>{
让result=JSON.parse(数据);
var y=结果类型;
回调(null,y);
});
}).on(“错误”,(错误)=>{
日志(“错误:+err.message”);
});
}
我正试图通过Android应用程序捕捉其输出/返回值,使用:
private static FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
mFunctions.getHttpsCallable(nameOfFunction).call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
String result2 = (String) task.getResult().getData();
return result2;
}
});
private静态FirebaseFunctions mffunctions=FirebaseFunctions.getInstance();
GetHttpScalable(nameOfFunction).call(数据)
.continueWith(新的continueWith(){
@凌驾
公共字符串(@NonNull Task)引发异常{
字符串result2=(字符串)task.getResult().getData();
返回结果2;
}
});
然而,Android代码中的result2
变量总是返回null,即使云函数工作正常
我哪里弄错了?因为
otpValidation
也在调用HTTP API,所以需要让它返回一个承诺,然后将承诺“泡泡”到云函数之外。现在您没有从顶层代码返回任何内容,这意味着在运行最终的}
后,云函数可能会随时关闭代码,这很可能是在HTTP调用仍在运行时(更不用说createUser
调用了)
第一步是让otpValidation
返回承诺,并解决/拒绝该承诺:
otpValidation(phoneNumber:string, otp:string): Promise<Any> {
let otpValidationApi:string = "https://<api>/verifyRequestOTP.php?authkey="+this.authKey+"&mobile="+phoneNumber+
"&otp="+otp;
return new Promise(function(resolve, reject) {
https.get(otpValidationApi, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
let result = JSON.parse(data);
var y=result.type;
resolve(y);
});
}).on("error",(err)=>{
console.log("Error: "+err.message);
reject(err);
});
});
}
在阅读其余代码时,您可能还需要转换
authFunction.createUser
以返回承诺。如果是这样,方法将与我在上面为您的auth.otpValidation
所做的相同。什么是authFunction.otpValidation
?@frankvanpoffelen编辑了添加authFunction.otpValidation方法的问题。这很有效。。谢谢您的帮助,弗兰克……我必须说,firebase开发团队非常努力地支持其用户:)我们喜欢帮助其他开发人员。您使用Firebase只是一种奖励。;-)
exports.validateOtp = functions.https.onCall((data,context)=>{
phoneNumber = data.phoneNumber;
otp = data.otp;
let email:string = data.email;
let password:string = data.password;
let displayName:string= data.displayName;
return authFunction.otpValidation(phoneNumber,otp).then(function(optValue) {
return authFunction.createUser(email,false,phoneNumber,password,displayName,false,(err,value) => {
if(err)
{
console.error(err);
return Promise.reject(err);
}
else{
console.log(value);
return Promise.resolve(value);
}
});
}
});