I'm getting a wrong postKey from push(), when I'm trying to get the key when I'm uploading the post

Leo S :

When uploading the post, I'm trying to get the postKey and upload it to dynamiclink, but I'm getting wrong postKey (first 6 characters in the key are right then the rest are wrong). How can I get the right postKey when uploading the post?

This is my post including the right postKey:

post image

And this is what I'm getting -LYelE9TdCU73qW1Xisc , which also is not a key for any other posts.

My code

String postKey = FirebaseDatabase.getInstance().getReference().child("posts").push().getKey();
        Log.i("postkey for dynamiclink",postKey);

Very much appreciated your help!

Peter Haddad :

In your code, you are creating another postKey by using push().getKey().

To retrieve the key, that is already in the database, try the following:

  DatabaseReference reference = FirebaseDatabase.getInstance().getReference("posts");

reference.orderByChild("title").equalTo("test").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot datas: dataSnapshot.getChildren()){
     String keys=datas.getKey();
    }
 }
@Override
public void onCancelled(DatabaseError databaseError) {
    }
 });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160191&siteId=1