Poe Eain
Poe Eain

Reputation: 83

Vue, firebase database return the empty array

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

Upvotes: 0

Views: 750

Answers (1)

wobsoriano
wobsoriano

Reputation: 13434

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

Upvotes: 1

Related Questions