1
I’m having problems with javascript react-native
, need to store the return of firebase
using a for
in a variable and access it externally, but the fact is that nothing is returning.
export const atualizadorChamados = () => {
const { currentUser } = firebase.auth();
const emailCripto = b64.encode(currentUser.email);
const ref = firebase.database().ref();
let itensAbertos = [];
let itensFinalizados = [];
return dispatch => {
dispatch({ type: 'carrega_chamados_abertos' });
dispatch({ type: 'carrega_chamados_finalizados' });
ref.child(`/chamados/${emailCripto}`)
.on('value', (snap) => {
itensAbertos = [];
itensFinalizados = [];
this.nomeResponsavel = 'Jurema';
snap.forEach((c) => {
firebase.database().ref(`/usuarios/${c.val().responsavel}`)
.on('value', s => {
s.forEach((child) => {
this.nomeResponsavel = child.val();
});
});
console.log(this.nomeResponsavel);
if (c.val().status === 0) {
itensAbertos.push({
assunto: c.val().assunto,
data: c.val().data,
responsavel: c.val().responsavel,
status: c.val().status,
setor: c.val().setor,
id: c.val().id,
nomeResponsavel: this.nomeResponsavel,
_key: c.key
});
} else {
itensFinalizados.push({
assunto: c.val().assunto,
data: c.val().data,
responsavel: c.val().responsavel,
status: c.val().status,
setor: c.val().setor,
id: c.val().id,
nomeResponsavel: this.nomeResponsavel,
_key: c.key
});
}
});
dispatch({ type: 'lista_chamados_abertos', payload: itensAbertos });
dispatch({ type: 'lista_chamados_finalizados', payload: itensFinalizados });
});
};
};
Keeps coming back 'Jurema'
instead of the object.
Hello @Welcome to Sopt, the code placed looks this incomplete and as you are using
this
it gets a little harder to point out a solution. And recommended you add at least enough for a test - [MCVE]. -- About your question you may be having trouble with precisely thethis
try to putvar self = this
at the beginning and then use theself
instead ofthis
.– Icaro Martins
Thanks @Icaromartins, I put the code as suggested, I tried to do earlier with a variable
let
but as it was not working I tried to use thethis
– Gustavo Machado
React or React-Native?
– sant0will
React-Native, edited, edited.
– Gustavo Machado
I think this is happening because of the reading methods of
Firebase
occur asynchronously.– Gustavo Machado