Problem with variable javascript with firebase

Asked

Viewed 73 times

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 the this try to put var self = this at the beginning and then use the self instead of this.

  • 1

    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 the this

  • React or React-Native?

  • React-Native, edited, edited.

  • I think this is happening because of the reading methods of Firebase occur asynchronously.

1 answer

0

Try this... I didn’t have time to execute the code... It may be that the code does not work but it will serve as a basis to understand how the logic is made

export const atualizadorChamados = () => {
    const { currentUser } = firebase.auth();
    const emailCripto = b64.encode(currentUser.email);
    const ref = firebase.database().ref();
    return dispatch => {
        dispatch({ type: 'carrega_chamados_abertos' });
        dispatch({ type: 'carrega_chamados_finalizados' });
        ref.child(`/chamados/${emailCripto}`)
            .on('value', (snap) => {

                let itens = () => snap.map((child) => {
                    let responsavel = null
                    firebase.database().ref(`/usuarios/${c.val().responsavel}`)
                        .on('value', s => {
                            //ESSA PARTE AQUI FICA DIFICIL DE ENTENDER QUAL É O DADO QUE RETORNA
                            // SE FOR UM OBJETO COM O NOME DO CARA PODE SER ISSO
                             responsavel = s.val().nomeResponsavel
                        })
                    child.val() && {
                        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: responsavel,
                        _key: c.key
                    }

                })
                dispatch({ type: 'lista_chamados_abertos', payload: itens.filter(item => item.status === 0) });
                dispatch({ type: 'lista_chamados_finalizados', payload: itens.filter(item => item.status !== 0) });
            });
    };
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.