Error when displaying Firebase + Vuejs + ElectronJs data

0

I can not see data with electronJs and Vuejs from Firebase.

let usuarios = mdb.ref("usuarios");
var vm = this;   
var user = firebase.auth().currentUser;
var userid = "";
firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
        userid = user.uid
        console.log("ID : " + user.uid);
        console.log("EMAIL : " + user.email);
        console.log("userid : " + userid);
        var usuariosdata = mdb.ref("usuarios").child(userid);        
        console.log("usuariosdata : " + usuariosdata);
    } else {
        console.log("ES totalemente nulo")
        var usuariosdata = null 
    }
});

From console.log in onAuthStateChanged I can see the user's data by console, but when I try to execute console.log from outside of onAuthStateChanged it shows me a null data. Why?

usuariosdata is the reference I need to show my data.

    
asked by Nahuel Jakobson 02.10.2018 в 01:21
source

1 answer

1

That's because Firebase only passes the user parameter within the same function, if what you want is to show the data of the current user, place the following variable within the functions in which you need this data:

var user = firebase.auth().currentUser;

And you can use it within each function declaring it inside it or you can do it globally. As in the following examples (in this case represented in JQuery):

$("mietiqueta").on("click", function(){
 var user = firebase.auth().currentUser;
 console.log(user.uid);
});

O

var USER = firebase.auth().currentUser;
$("mietiqueta").on("click", function(){
 console.log(USER.uid);
});

In the function onAuthStateChanged(user) as I mentioned, you can do it thanks to the parameter of this, but to do it in another site just use the necessary instance of Firebase Auth in the place where you want to use it.

I'll leave you here: Get a user profile - Firebase

I hope you have explained me well and that it will help you.

  

EDIT:

If what you want instead is to enter or show the data of your DB, it is worth noting that you must have saved it beforehand, otherwise it will return you null because there is no such data in the database.

A small guide would be the following:

  

Save your users' data in the database.

Here we save the data within the node users/uidDelUsuario

//Crea la referencia a la base de datos 
var db = firebase.database();
//Crea una referencia con el uid del usuario.
var user = firebase.auth().currentUser;
var userId = user.uid;
//Crea una referencia al lugar en donde quieres almacenar con el id 
//correspondiente
db.ref("users/"+userId).set({
 Nombre: user.displayName,
 Id: user.uid,
 etc...
});

We already have the user data stored in the database.

  

We require the user's data in our interface (console).

Here we call the data that is inside the node users/uidDelUsuario

//Usamos las referencias de la misma manera
var user = firebase.auth().currentUser;
var userId = user.uid;
var db = firebase.database();
// Solicitamos los datos de la base de datos con una snapshot
db.ref("users/"+userId).on("value", function(snapshot){
 //Utilizamos nuestra snapshot de la siguiente manera
 console.log(snapshot.val())
 //O
 var data = snapshot.val();
 console.log(data.Nombre);
 console.log(data.Id);
 etc...
});

Here I leave you: Read and write data on the Web

Again I hope it will be helpful, the contents can be found more complete in the documentation within the links that I have left you.

    
answered by 02.10.2018 в 05:23