0

I am trying to retrieve the data from firebase database.

And I can connect to firebase auth and check currentUser data, it's fine.
But I can't retrieve the data from firebase database, it just return the empty array.

firebase version = 5.0.4
vue version = 2.5.16

The database structure

users/     
--------uId/        
--------------name    
--------------email 

Vue Js folder structure

App.vue    
firebase.js    
main.js     
components /     
------------ Home.vue  

firebase.js

var firebase = require('firebase');
require("firebase/auth");     
require("firebase/database");

var config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "..."
};
firebase.initializeApp(config)
export const auth = firebase.auth();
export const db = firebase.database()

Home.vue

<script>
import {db} from './../firebase';
import {auth} from './../firebase';
var usersRef = db.ref('users');

export default {
    name: "Home",
    components: {
        add_new
    },
    data(){
        return {}
    },
    mounted: function(){
        console.log(usersRef);
        var userId = auth.currentUser.uid;
        return db.ref('/users/' + userId).once('value').then(function(snapshot) {
            snapshot.forEach(function (childData) {
                console.log(childData.val())
            })
        })
    }
}
</script>

Here, Vue app works correctly,
only the firebase.database ( usersRef ) returns the empty array.

This is the screenshot of console.log(usersRef) result
https://i.sstatic.net/NH7gh.png

2
  • You are sure that userId has the right value? Because nowhere in your code appears the login part. If you do console.log(userId) do you get the correct value? Commented Jun 10, 2018 at 10:18
  • Yes I got the correct value such as "jzeagloTU8Qjpfxo....", And I open the database for both read and write. Commented Jun 10, 2018 at 10:33

1 Answer 1

1

First instead of using multiple import statements, why not

import {auth, db} from './../firebase';

And try to change

snapshot.forEach(function (childData) {
   console.log(childData.val())
})

to snapshot.val()

var userId = auth.currentUser.uid;
return db.ref('/users/' + userId).once('value').then(function(snapshot) {
   console.log(snapshot.val());
});
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry @FewFlyBy, I tried the same as your answer like console.log(snapshot.val()); , but it returns null.
do you get the value of userId when you console log it?
Thanks @FewFlyBy, it was my mistakes, I logged in with a wrong user that has no data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.