Attribute json Undefined

Asked

Viewed 93 times

0

Good afternoon, everyone,

I’m having a problem that I can’t access the APPLICATION attribute of json, he always returns undefined.

When executing console.log(aplic[0]) he prints this json, but if I give a console.log(aplic[0].APLICACOES) it does not show the 4 child objects of applications.

Does anyone know why this is happening? or what I should do to solve this problem.

Thank you

json

function getAplicacoesAtividadesAgricolasBD(idAtividadeAgricola) {
return new Promise((resolve, reject) => {
    const aplicacoes = [];
    db.transaction((tx) => {
        tx.executeSql('SELECT * FROM ATIVIDADE_AGRICOLA_APLICACAO WHERE ID_ATIVIDADE_AGRICOLA = ?',
            [idAtividadeAgricola], (tx, results) => {
                for (let i = 0; i < results.rows.length; i++) {
                    const aplic = results.rows.item(i);
                    aplicacoes.push(aplic);
                }

                resolve(aplicacoes);
            }, () => {
                reject('Erro ao consultar dados. Por favor, tente novamente!');
            });
    });
});


function getDataDBAtividadeAgricolaById(idPropriedade, idAtividadeAgricola) {
return new Promise((resolve, reject) => {
    const aplicacoes = [];
    db.transaction((tx) => {
        tx.executeSql('SELECT * FROM ATIVIDADE_AGRICOLA WHERE (ID_PROPRIEDADE = ?) AND (ID_ATIVIDADE_AGRICOLA = ?)',
            [idPropriedade, idAtividadeAgricola], (tx, results) => {
                for (let i = 0; i < results.rows.length; i++) {
                    const aplic = results.rows.item(i);

                    getAplicacoesAtividadesAgricolasBD(aplic.ID_ATIVIDADE_AGRICOLA)
                        .then(itens => {
                            aplic['APLICACOES'] = itens;
                        });
                    aplicacoes.push(aplic);
                }

                resolve(aplicacoes);
            }, (err) => {
                reject('Erro ao consultar dados. Por favor, tente novamente!');
            });
    });
});

export function visualizarAtividadeAgricolaModal(idPropriedade, idAtividadeAgricola) {
return async (dispatch) => {
    getDataDBAtividadeAgricolaById(idPropriedade, idAtividadeAgricola)
        .then(aplic => {
            console.log(aplic[0]);
            console.log(JSON.stringify(aplic[0]));
            console.log(aplic[0].APLICACOES);
            dispatch({
                type: AGR_AA_SET_ATIVIDADE_AGRICOLA_VIEW,
                data: aplic[0]
            });
        })
        .catch((err) => {
            console.log(err);
            Alert.alert('Atenção!', 'Erro ao consultar dados. Por favor, tente novamente!');
        });
};
  • console.log(JSON.stringify(aplic)). Send us the result of this to see

  • Blza. already put the code and n image. As for the output of console.log(JSON.stringify(aplic)), actually here does not contain the attribute APPLICATIONS.. Let me post the code I’m using that looks better for you to help me. Could it be some Redux sync?

  • Ever tried to pass x = aplic[0].APLICACOES a variable and that variable take x[0] ?

1 answer

0

The

aplic[0].APLICACOES

contains an array. Therefore it would be necessary to use a map function to display the elements. Something like this:

Object.keys(aplic[0].APLICACOES).map((key) => console.log(aplic[0].APLICACOES[key]))

Browser other questions tagged

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