java - Firebase OrderbyChild 查询返回父键

标签 java android firebase firebase-realtime-database

我在使用 orderByChild().equalTo() 方法检索和删除 Firebase 数据库中节点的正确级别时遇到问题。

在网上搜索了好久,并没有真正解决我的问题。请帮忙。

我的数据库结构:

enter image description here

My Firebase 添加和删除照片的方法

private void addPhotoToDatabase(String url){
    Log.d(TAG, "addPhotoToDatabase: adding photo to database");

    String newPhotoKey = myRef.child(mContext.getString(R.string.dbname_user_photos)).push().getKey();
    Photo photo = new Photo();
    photo.setDate_created(getTimeStamped());
    photo.setImage_path(url);
    photo.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
    photo.setPhoto_id(newPhotoKey);

    //insert into database.
    myRef.child(mContext.getString(R.string.dbname_user_photos)).child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(newPhotoKey).setValue(photo);

}

private void deletePhotoFromDatabase(String image_path){
    Log.d(TAG, "deletePhotoFromDatabase: deleting photo from database");
    Log.d(TAG, "deletePhotoFromDatabase: image_path is " + image_path);

    String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
    myRef.child(mContext.getString(R.string.dbname_user_photos))
            .child(user_id)
            .orderByValue().equalTo(image_path).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

                String photoKey = dataSnapshot.getKey();
                Log.d(TAG, "onDataChange: photo key is " + photoKey);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

我知道我还没有 removeValuesetValue(null) 方法,因为我无法达到正确的级别。我试图检索的 photoKey 是 -L-FnLvNxLauiue4InSxE,它是在添加到 Firebase 时自动生成的。但是,日志总是返回到其父节点,即 UID

顺便说一下,我从正确记录的 Firebase 存储中检索到的图像路径。

有人能给我一些我做错的灵感吗?非常感谢!

最佳答案

改变这个:

 myRef.child(mContext.getString(R.string.dbname_user_photos))
        .child(user_id)
        .orderByValue().equalTo(image_path)

为此:

 DatabaseReference myRef=FirebaseDatabase.getInstance().getReference();
 myRef.child("user_photos").child(user_id).orderByChild("photo_id").equalTo(image_path);

我假设 user_photos 是 root 下的直接 child ,因此使用上面的 myRef

编辑:

 @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
     for(DataSnapshot data : dataSnaphot.getChildren()){

            String photoKey = data.getKey();
            Log.d(TAG, "onDataChange: photo key is " + photoKey);

    } }

关于java - Firebase OrderbyChild 查询返回父键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47588062/

相关文章:

Android 错误 : "Could not create epoll instance", 或 "Could not create wake pipe"

javascript - Firebase实时数据库如何根据网络应用程序中的可用性过滤数据

java - 始终调用 Thread.currentThread().interrupt();捕获 InterruptedException 时?

java - 如何获得对 INT_MAX 求模的结果?

java - 服务器上有没有生成JSESSIONID的公式?

android - setText 不会将文本设置为 EditText

android - 当我关闭我的应用程序并重新打开它时,下载和导入的 Json Are Gone

swift - 初始化器 'init(_:)' 要求 '' 符合 'StringProtocol' SwiftUI Picker with Firebase

ios - 区分应用程序登录和 Web 登录到 Firebase

java - 如何编写Java程序来打开特定的文件扩展名(.pef)?