Fetching dataquery

Hi All,
I tried to run data query resource:analytics.
But the result console undefined
my code below:

const myanalytic = {
results: {
resource: ‘analytics’,
params: {
dimension: [‘dx:y1gIMkQteQr’,‘pe:LAST_12_MONTHS’],
filter:‘ou:vUs2cBkPn50’
}
}
}
const { loading, error, data } = useDataQuery(myanalytic);
{console.log(“analytics:”+data)}

thank you

Hello @Channara

Here is something that might help.

If the data of the useDataQuery hook is undefined, it either means it is still loading (loading == true) or there is an error with the query. The latter is most likely. You could try logging the error or loading to see if the query had issues or is still loading.

This is from the DHIS2 App Runtime Documentation

Regards.

Hi @nnkogift
I follow instruction step link, Yes it works fine for programs or indicators. But when i use resource:analytics. it show undefined.

@Channara

What does console.log(error) print?

Hi,
no error display.
but i use many useDataQuery hook in one function, can be that problem?

Hello

Do you mean you use the useDataQuery hook like this?

const Component = () =>{
     const { data: data1, error: error1, loading: loading1} = useDataQuery(query1)
      const { data:data2, error: error2, loading:loading2} = useDataQuery(query2)

...
}

This may work (assuming the variables for error, loading, and, data are not mixed up). There is a simpler way, however, of combining such queries. You could use only one useDataQuery hook with the two queries passed in one object, they just need to have different keys. An example;

const queries = {
    indicators: {
        resource: 'indicators',
        params: {
            fields: ['id', 'displayName']
        }
    },
    analytics: {
        resource: 'analytics',
        params: {
            dimension: ['dx:y1gIMkQteQr', 'pe:LAST_12_MONTHS'],
            filter: [
                'ou:vUs2cBkPn50'
            ]
        }
    }
}

const Component = () =>{
    const {error, loading, data} = useDataQuery(queries)
    
    //data will contain an object of all your queries' results
}

Perhaps you could try implementing the queries this way and see if the problem is still there.

2 Likes

Yes, i use many useDataQuery, it doesn’t show any error, but the result is undefined.
when i combine in one useDataQuery, yes sure, it works fine.

many thank for supporting.

2 Likes

Welcome :pray:t5:

2 Likes