0

I am trying to use the Firebase Admin SDK to save values to a Firebase real-time database, but no matter what I do, none of the values are being saved. Unlike the tutorials, I am starting out with a completely empty database:

enter image description here

  • I am connected to the database (verified using .info/connected reference, and ref.addValueEventListener)
  • Security rules are allow both read and write
  • Using a service account, and the json file for it seems to be parsed and loaded correctly
  • If I create a root reference object, and print its value, it is correctly pointing to the database url

Firestore has a collection and document created, and if I switch to the real-time database from the Firestore view, the database also has this collection and document.

enter image description here

But if I press on the root link, the document and collection disappears. I am also constantly seeing "data" as null. Is data a document? A field? Is it just saying the database as a whole is empty? Sometimes it alternates between "data: null" and "coconut-xxx:null" which is odd.

I have also tried to reference /telemetry/data and write to it, but that didn't work. When I add a CompletionListener to setValue, the CompletionListener never seems to be called, so the connection is hanging or something?

My code:

String url = databaseUrl.getProtocol() + "://" + databaseUrl.getHost();

InputStream stream = getClass().getClassLoader().getResourceAsStream("firebaseCredentials.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(stream);

FirebaseOptions options = new FirebaseOptions.Builder()
                                             .setCredentials(credentials)
                                             .setDatabaseUrl(url)
                                             .build();

FirebaseApp.initializeApp(options);

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.setValueAsync("12345");

I have also tried to send a Map like they do in the tutorial, but that doesn't work either.

I guess the only other thing that might be an issue is that I am doing this inside of an IntelliJ Android Studio plugin. That seems highly unlikely as a problem though..

Any help is appreciated. Thanks!

3
  • Hmm.. I am taking a look at this "app" thing. Right now it looks like I am using the default app. The Firebase console shows I have two projects, one of which is coconut. I had no apps added to it.. does that matter? None of the available apps match what I am doing, though. Commented Nov 13, 2019 at 16:29
  • 1
    Not a java developer, but in Node.js typically 'push, update, set' are used to insert records to Firebase in key value pair, something like this firebase.database().ref("root node").push({ ChatStartTime: new Date().toLocaleString() });
    – ASP
    Commented Nov 13, 2019 at 16:44
  • Thanks for the comment ASP! setValueAsync is the set operation, and I also tried setValue. I have tried push once, but not update. I would think if neither set or push were working, that update won't either. I did just try it, but it doesn't seem to make a difference. Commented Nov 13, 2019 at 16:51

1 Answer 1

1

I found the problem. I don't know how this happened, but there was a mismatch between the service account JSON file in my IntelliJ resources folder, and the service account being used for the coconut project...

Anyway, the real-time database is working now!

Not the answer you're looking for? Browse other questions tagged or ask your own question.