简体   繁体   English

如何使用 Firebase 检索已登录用户的 UID 下的数据

[英]How do I retrieve data under UID for the signed in user with Firebase

I saved some data under a node with the currently signed in user UID, along with the post timestamp.I successfully manage to retrieve all the data under this node, but I would only like to retrieve the data for the currently signed in user, I am trying to create something similar to how amazon stores an item in the cart and when the user clicks the cart it shows the items they have added there, not all the items for every user.我在一个节点下保存了一些数据,该节点具有当前登录的用户 UID,以及发布时间戳。我成功地设法检索了该节点下的所有数据,但我只想检索当前登录用户的数据,我我正在尝试创建类似于亚马逊在购物车中存储商品的方式,当用户单击购物车时,它会显示他们已添加到那里的商品,而不是每个用户的所有商品。 Can someone assist me with this problem?有人可以帮我解决这个问题吗?

我的数据库

public void addItemToCart(){

    SimpleDateFormat formatter = new SimpleDateFormat(" HH:mm:ss ");
    Date date = new Date(System.currentTimeMillis());
    timeStamp = loggedInUserId + formatter.format(date);


    userDictionary.put("itemPrice", selectedPrice);
    userDictionary.put("productname",productNameText);
    userDictionary.put("description", selectedStringProductDescrtipon);
    userDictionary.put("uid",loggedInUserId);
    userDictionary.put("productImage", selectedImage);
    userDictionary.put("datee",date.toString());
    userDictionary.put("name",loggedInUserName);
    userDictionary.put("timestamp",timeStamp);




    uploadPostRef.child("Cart").child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void unused) {
            Log.i("sucessfully added","sucesssfully added to cart..");
            getLoggedInUserData();

        }
    });
    numberInCartIv.setVisibility(View.VISIBLE);

    public void downloadCartData(){
        cartDb.child("Cart").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    if (data.exists()) {

                        Log.i("data",data.toString());

//                        cartModel = data.getValue(CartModel.class);
//                        cartModelArrayList.add(cartModel);
//                        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
//                        cartRecyleView.setLayoutManager(horizontalLayoutManager);
//                        cartAdapter  = new CartAdapter(cartModelArrayList, getContext());
//                        cartRecyleView.setAdapter(cartAdapter);


                    } else {
                        Log.i("error", "Error Loading JSON data..");

                    }

                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                String error = databaseError.getDetails();
                Log.i("error", error);

            }
        });

    }
  1. First of all, you need to change your DB json to this one.首先,您需要将您的 DB json 更改为这个。

    Cart -userUid -cartUid -datee -description -andSoOn购物车 -userUid -cartUid -datee -description -andSoOn

  2. That's mean, when you are storing the item into Cart .这意味着,当您将商品存储到Cart中时。 You need to include the userUid .您需要包括userUid

    //Getting userUid final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); //获取 userUid final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser(); if (firebaseUser.= null) { final String userUid = firebaseUser;getUid().如果 (firebaseUser.= null) { final String userUid = firebaseUser;getUid()。 uploadPostRef.child("Cart").child(userUid).child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void unused) { Log,i("sucessfully added"."sucesssfully added to cart.;"); getLoggedInUserData(); } }); uploadPostRef.child("Cart").child(userUid).child(timeStamp).setValue(userDictionary).addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void unused) { Log,i("添加成功"。 “成功添加到购物车。;”); getLoggedInUserData(); } }); } }

  3. To retrieve the Cart according to the user.根据用户检索Cart You just call them like this.你就这样称呼他们。

    public void downloadCartData(){ cartDb.child("Cart").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot data: dataSnapshot.getChildren()) { if (data.exists()) { public void downloadCartData(){ cartDb.child("Cart").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot data: dataSnapshot.getChildren()) { 如果(data.exists()) {

     Log.i("data",data.toString()); } else { Log.i("error", "Error Loading JSON data.."); } } } @Override public void onCancelled(DatabaseError databaseError) { String error = databaseError.getDetails(); Log.i("error", error); } });

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检索作为文档放置在 Firebase Cloud Firestore 中的 UID - How do I retrieve the UID's that I have placed as documents in my Firebase Cloud Firestore UID下如何存储Firebase用户 - How To Store Firebase Users Under UID 使用 Cloud Functions 访问 firestore 中的用户数据,但如何安全地发送 UID/IDToken? - Accessing user data in firestore using Cloud Functions, but how do I securely send the UID/IDToken? 如何通过 flutter 中的文档从 firebase 存储中检索数据? - How do i retrieve data from firebase store by document in flutter? 在 Flutter 中获取当前用户 uid 为 Firebase 的数据 - Fetch Firebase data with current user uid in Flutter C#用户登录email时如何获取用户UID - How to get user UID when user is signed in with email in C# 我无法从特定用户获取 firebase 的数据(使用 uid 检查方法) - I can not get data from firebase from a specific user (using the uid check method) 如何使用 FirebaseRecyclerOptions 检索 uid 数据 - How to retrieve uid data using FirebaseRecyclerOptions 从 firebase 数据库 flutter 中的 uid 获取用户数据 - get user data from uid in firebase database flutter Firebase 实时 - 如何检索由 push() 生成的数据? - Firebase Realtime - How do I retrieve data that has been generated by the push()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM