Select to view content in your preferred language

Trying to query a datasource is a custom widget, never returns any results

1612
4
Jump to solution
02-02-2023 12:41 PM
PaulNRWA
Occasional Contributor

I'm attempting to create a custom widget and struggling to query data from a feature layer.  When I step thru the debugger, everything works, but no data rows are ever returned.   The debugger shows the DataSource does indeed exist and everything looks fine, I simply cannot get any data to return.   Here is a stripped down version of the code:

let qryParamsNow = [] as SqlQueryParams;
qryParamsNow.where = '1=1'
qryParamsNow.outFields = ['*']
let dataPortalUser = DataSourceManager.getInstance().getDataSourcesAsArray()[0] as QueriableDataSource
         
dataPortalUser.query(qryParamsNow)
.then((response) =>
{
   const record = response.records[0];
   console.log(record.getFieldValue('email'));
})
.finally()
{
  // Any records?
  const dtrResults = dataPortalUser.getRecords()
  console.log(dtrResults[0].getFieldValue('email'));
}
 
As I step thru the debugger, I keep an eye on the network and that layer is never accessed.   When it reaches the '.finally' the message shows unloaded.   Prior to that the response message is 'pending'.
 
Thanks in advance.
0 Kudos
1 Solution

Accepted Solutions
Grant-S-Carroll
Esri Contributor

Hi There,

Not sure about casting to QueriableDataSource (I've not used it in my workflows), I cast to a FeatureLayerDataSource and then query from there. The other recommendation I would make is to always include a catch in your promises, as it will allow you to see any errors generated from the server, otherwise they can fail silently. See code snippet below

let dsm = DataSourceManager.getInstance();
        // use the settings to get the datasource by id.
        let ds = dsm.getDataSource(this.props.useDataSources[1].dataSourceId) as FeatureLayerDataSource;  
        //I'm not creating a seperate object for query parameters, just passing them in like so
        ds.query({ where: '1 = 1', returnGeometry: false, pageSize: 2 })
            .then((response) =>
            {
                //Log the respone
                console.log(response)
            })
            .catch((error) => {
                //Catch any issues and log to the console
                console.error(error)
            })
            .finally(() => {
                console.log('Operation completed')
            })

 

Hopefully this helps.

Cheers

View solution in original post

4 Replies
Grant-S-Carroll
Esri Contributor

Hi There,

Not sure about casting to QueriableDataSource (I've not used it in my workflows), I cast to a FeatureLayerDataSource and then query from there. The other recommendation I would make is to always include a catch in your promises, as it will allow you to see any errors generated from the server, otherwise they can fail silently. See code snippet below

let dsm = DataSourceManager.getInstance();
        // use the settings to get the datasource by id.
        let ds = dsm.getDataSource(this.props.useDataSources[1].dataSourceId) as FeatureLayerDataSource;  
        //I'm not creating a seperate object for query parameters, just passing them in like so
        ds.query({ where: '1 = 1', returnGeometry: false, pageSize: 2 })
            .then((response) =>
            {
                //Log the respone
                console.log(response)
            })
            .catch((error) => {
                //Catch any issues and log to the console
                console.error(error)
            })
            .finally(() => {
                console.log('Operation completed')
            })

 

Hopefully this helps.

Cheers

PaulNRWA
Occasional Contributor

Thanks for the help Grant.   I used that exact code and set a breakpoint to debug the steps.   When I stepped thru the ds.query, it blew right past all the statements and went to the end of my function.   When I examine the "ds" properties in developer tools, everything looks fine.   URL, schema, etc.   I verified that data does indeed exist in the layer.    

I'll keep at it.   I know it's something on my end and it's been a while since I've done much development.

0 Kudos
Grant-S-Carroll
Esri Contributor

Hi Paul

Unless you set a break point on the return statements, eg console.log etc, it will pass over it as its an async process, so it will run the code in the background after which it will jump in to either the then/catch functions. 

Try that and see if you start to see any results?

Cheers

 

 

0 Kudos
PaulNRWA
Occasional Contributor

Grant - thank you so much!  I had async in the back of my head, but was too impatient.   All is now working and I can move on.   

I appreciate the help.

0 Kudos