larpon / QtFirebase

An effort to bring Google's Firebase C++ API to Qt + QML
MIT License
283 stars 83 forks source link

Getting Value from Firebase Real-time Database (Qt c++) #143

Closed shariatraad closed 4 years ago

shariatraad commented 4 years ago

I am trying to get a value from my Firebase real-time database but I keep getting 0 for firebase::kFutureStatusPending (firebase::kFutureStatusPending tells me my results are still pending ). I am able to set value by using SetValue() on my database and I have checked the url with dbref.Child(user->uid()).Child("Nickname").url() and it is correct. here is a piece of my code related to this part and I also have included JSON structure at the end:

firebase::database::Database *database=firebase::database::Database::GetInstance(_app);
dbref = database->GetReferenceFromUrl("https://***/");

firebase::Future<firebase::database::DataSnapshot> result =
    dbref.Child(user->uid()).Child("Nickname").GetValue();

if (result.status() != firebase::kFutureStatusPending) {
  if (result.status() != firebase::kFutureStatusComplete) {
    qDebug() <<"ERROR: GetValue() returned an invalid result.";

  } else if (result.error() != firebase::database::kErrorNone) {
    qDebug() << result.error_message();

  } else {
    firebase::database::DataSnapshot snapshot = *result.result();
    qDebug() << "snapshot available" ;
  }
}

else {
    qDebug() << "results are still pending";
}

How can I get the value of "Nickname" child (which is "Raad" in this case) in my Qt Android app?

Here is the JSON file content :

{
  "YQEa5KquWgOiPHfD7SLSgU92mTH2" : {
    "Email Address" : "shariatraad@gmail.com",
    "Nickname" : "Raad"
  }
}
shariatraad commented 4 years ago

I solved the issue by adding this to the code in case if anyone faced the same issue:

result.OnCompletion([](const firebase::Future<firebase::database::DataSnapshot>& completed_future){
    if (completed_future.error() == 0)
    {
       //Do what you need to do with your value here
    }
}
);