问题
I am using Android.
After the user logs in to firebase, how can I retrieve the user information in the firestore according to the account?
My function is in the cloud function of firebase. My code as follows:
mFunctions.getHttpsCallable("getInfo").call()
.continueWith(new Continuation<HttpsCallableResult, Object>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
Log.d("-----TEST-----", "BEGIN000");
Object result = task.getResult().getData();
Log.d("-----TEST-----", "SUCCESS");
self_tel.setText((Integer) result);
return (String) result;
}
}).addOnCompleteListener(new OnCompleteListener<Object>() {
@Override
public void onComplete(@NonNull Task<Object> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
Object details = ffe.getDetails();
}
// [START_EXCLUDE]
Log.w("test", "addMessage:onFailure", e);
//showSnackbar("An error occurred.");
return;
// [END_EXCLUDE]
}
String result = (String) task.getResult();
Log.d("-----TEST-----", "SUCCESS1");
}
});
回答1:
If you want assistance using firebase-firestore you can use the inbuild assistant.
Goto Tools -> Firebase
and follow the instructions.
You can pass parameters to the firebase function using HashMap
// Create the arguments to the callable function.
Map<String, Object> data = new HashMap<>();
data.put("text", text);
data.put("push", true);
and pass it using .call(data)
// Create the arguments to the callable function.
Map<String, Object> data = new HashMap<>();
data.put("text", text);
data.put("push", true);
return mFunctions
.getHttpsCallable("getInfo")
.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.
String result = (String) task.getResult().getData();
return result;
}
});
For more instructions Call functions from your app
来源:https://stackoverflow.com/questions/51888994/how-to-pass-parameters-to-the-cloud-function-of-firebase