Boa tarde pessoal, sou novo no fórum e também em javascript, mas vou tentar ser o mais breve possível.
Eu tenho uma função que me retorna um array, essa função é chamada no construtor do meu service onde é atribuído a outra variável, assim:
getValor(){
const key: string = this.afAuth.auth.currentUser.uid;
const PATH: string = 'usuarios/';
let fbRef: any;
fbRef = this.db.database.ref(PATH + key);
var getListings = function () {
var currentItems =[];
var currentUser = [];
fbRef.on("value", function(snapshot) {
currentItems.push(snapshot.val());
currentUser.push({
nome: currentItems[0].nome,
celular: currentItems[0].celular,
email: currentItems[0].email,
idPerfil: currentItems[0].idPerfil,
photoURL: null,
emailVerificado: currentItems[0].emailVerificado
});
}),function (errorObject) {
console.log("The read failed: " + errorObject.code);
};
return currentUser;
};
return getListings();
}
Essa função getValor() é chamada no construtor do meu service, dessa forma:
constructor(
public: blablablalba...
) {
this.afAuth.authState.subscribe(user => {
if (user) {
this.userData = user;
this.userDataAuth.push(this.getValor());
this.userDataAuth.map(item => {
console.log(item.nome);
}
console.log('Data', this.userDataAuth[0]);
//for (var key in this.dataU) {
// console.log(this.dataU[key][key]);
//}
.
.
.
.
Obs: Esse log retorna undefined \/
console.log('Data', this.dataU[0].nome);
Enquanto esse retorna a lista de objetos \/
console.log('Data', this.dataU[0]);