how get reference in firebase



  • I am using firebase. I decided to have each users post under their uid, but somehow i cant make the reference and display it. I want to be able to display all data under every user uid.

    //this is my reference now :         
    var gettingPostRef = firebase.database().ref().child('Posts/');
    //if i use this it gives only the current login user posts:
    var gettingPostRef = firebase.database().ref().child('Posts/' + uid);
    

    but i would like to have all data under all uid to be displayed may i know how to make this reference properly,thanks.
    --- my code----firebase realtime-----

    var gettingPostRef = firebase.database().ref().child('Posts');
    gettingPostRef.on("child_added", function (znapshot) { // for getting all users posts
        root.child('users/').child(uid).once('value', function (userSnapshot) { //for getting users name
            var username = userSnapshot.child("Info/UserName").val().displayName;
            var gettingPost = znapshot.val();
            console.log(gettingPost)
            //checking for current users
            var arr = znapshot.val();
            var arr2 = Object.keys(arr);
            console.log("arr:" + arr2);
            //getting the value from database
            var url = gettingPost.url;
            var caption = gettingPost.caption;
            var date = gettingPost.Date;
            var title = gettingPost.title;
            console.log('File available at', url);
        })
    })
    

    ----this is my firebase structure----

    function myfunctionfirst() { $(".postingContent").on("click", function () {
    firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                // User is signed in.
                // select unique name for everytime when image uploaded 
                // Date.now() is function that give current timestamp 
                var name = "123" + Date.now();
    
                // make ref to your firebase storage and select images folder 
                var storageRef = firebase.storage().ref('/SocialPost/' + name);
                var uid = user.uid;
    
                // put file to firebase 
                var uploadTask = storageRef.put(selectedFile);
    
                // all working for progress bar that in html 
                // to indicate image uploading... report 
                uploadTask.on('state_changed', function (snapshot) {
                    var progress =
                        (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                    var uploader = document.getElementById('contentBar');
                    uploader.value = progress;
                    switch (snapshot.state) {
                        case firebase.storage.TaskState.PAUSED:
                            console.log('Upload is paused');
                            break;
                        case firebase.storage.TaskState.RUNNING:
                            console.log('Upload is running');
                            break;
    
                    }
                    document.getElementById('submit').setAttribute('disabled', 'true');
    
                }, function (error) {
                    console.log(error);
                }, function () {
                    //getting the time posted
    
                    var d = new Date();
                    var year = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                    var dd = d.getDate();
                    var mm = d.getMonth();
                    var month = year[mm];
                    var yyyy = d.getFullYear();
                    var today = month + " " + dd + ", " + yyyy
                    /////
                    var downloadURL = uploadTask.snapshot.downloadURL;
                    firebase.database().ref('Posts/' + uid).push({
                        url: downloadURL,
                        title: $("#name").val(),
                        caption: $("#message").val(),
                        user: uid,
                        Date: today,
                    });
                    // get the uploaded image url back 
                    uploadTask.snapshot.ref.getDownloadURL().then(
                        function (downloadURL) {
    
          document.getElementById('selected-images').src = downloadURL;
                        });
                });
    
            } else {
                // No user is signed in.
            }
        });
    });
    };
    


  • Hey @Abdullah

    Can you elaborate a little? For example, what you are getting back right now? What does your log say? etc?


Log in to reply