简体   繁体   中英

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

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:

发布图片

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!

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) {
    }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM