Java 为什么我的firestore函数在我的应用程序中返回null?
我试图弄明白为什么我的firestore函数总是返回null。 以下是我的firestore功能:Java 为什么我的firestore函数在我的应用程序中返回null?,java,android,promise,google-cloud-firestore,google-cloud-functions,Java,Android,Promise,Google Cloud Firestore,Google Cloud Functions,我试图弄明白为什么我的firestore函数总是返回null。 以下是我的firestore功能: exports.isUserOnListFunction = functions.https.onCall(async (data, context) => { const userID = context.auth.uid; const userEmail = context.auth.token.email; const atIndex = userEmail.i
exports.isUserOnListFunction = functions.https.onCall(async (data, context) => {
const userID = context.auth.uid;
const userEmail = context.auth.token.email;
const atIndex = userEmail.indexOf('@');
const dotIndex = userEmail.indexOf('.');
var userName = userEmail.slice(0, dotIndex);
var userSurname = userEmail.slice(dotIndex+1, atIndex);
userName = capitalizeFirstLetter(userName);
userSurname = capitalizeFirstLetter(userSurname);
return db.collection('allowed_users')
.where("name", "==", userName)
.where("surname", "==", userSurname)
.get().then(snap => {
if (!snap.empty) {
db.collection("users").doc(userID).update({
onList: true
});
return {onList : true};
}
}).catch(()=>{
console.log("Some error!")
})
});
private Task<String> isUserOnList(){
return mFunctions
.getHttpsCallable("isUserOnListFunction")
.call()
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
String result = (String) task.getResult().getData();
System.out.println(result);
return result;
}
});
}
下面是我的android(java)函数调用firestore函数:
exports.isUserOnListFunction = functions.https.onCall(async (data, context) => {
const userID = context.auth.uid;
const userEmail = context.auth.token.email;
const atIndex = userEmail.indexOf('@');
const dotIndex = userEmail.indexOf('.');
var userName = userEmail.slice(0, dotIndex);
var userSurname = userEmail.slice(dotIndex+1, atIndex);
userName = capitalizeFirstLetter(userName);
userSurname = capitalizeFirstLetter(userSurname);
return db.collection('allowed_users')
.where("name", "==", userName)
.where("surname", "==", userSurname)
.get().then(snap => {
if (!snap.empty) {
db.collection("users").doc(userID).update({
onList: true
});
return {onList : true};
}
}).catch(()=>{
console.log("Some error!")
})
});
private Task<String> isUserOnList(){
return mFunctions
.getHttpsCallable("isUserOnListFunction")
.call()
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
String result = (String) task.getResult().getData();
System.out.println(result);
return result;
}
});
}
private任务isUserOnList(){
返回函数
.GetHttpScalable(“isUserOnListFunction”)
.call()
.continueWith(新的continueWith(){
@凌驾
公共字符串(@NonNull Task)引发异常{
字符串结果=(字符串)task.getResult().getData();
系统输出打印项次(结果);
返回结果;
}
});
}
有人能告诉我我做错了什么吗?根据这个
若返回的是带有键和值的JS对象(我想在这种情况下是这样),则getData()返回Map对象
我认为应该使用task.getResult().getData().toString()
方法来转换它(而不是(String)task.getResult().getData();
)
我希望抛出异常,但可能您在未发布在此处的代码的其他部分中捕获了它
希望这对您有所帮助。根据此
若返回的是带有键和值的JS对象(我想在这种情况下是这样),则getData()返回Map对象
我认为应该使用task.getResult().getData().toString()
方法来转换它(而不是(String)task.getResult().getData();
)
我希望抛出异常,但可能您在未发布在此处的代码的其他部分中捕获了它
希望这对您有所帮助。您需要注意
update
返回的承诺,否则您的功能可能会在该工作完成之前提前终止。我已经做了一些研究,您是100%正确的。谢谢你抽出时间!:)您需要注意update
返回的承诺,否则您的功能可能会在该工作完成之前提前终止。我做了一些研究,您是100%正确的。谢谢你抽出时间!:)非常感谢您的回答!:)非常感谢您的回答!:)