Select to view content in your preferred language

relationQuery inside popupTemplate content function is not populating popup

997
5
01-25-2018 03:39 PM
JayHill
Frequent Contributor

But it returns my console logs correctly.

My content function is executing before the code to run the query on the related table finishes. Therefore returning blank values.

What is the best way to time this?

if (att.OBJECTID != null) {

                  var finalArray = [];
                  var objectID = att.OBJECTID;
                  var queryTask = new QueryTask({
                     url: "https://services.arcgis.com/blah/FeatureServer/0"
                  });

                  relationQuery = new RelationshipQuery({
                     objectIds: [objectID],
                     outFields: ["TYPE"],
                     relationshipId: 0
                   });
                   coreType = "";
                   myArray = [];
                   sampleTypes = "";
                   queryTask.executeRelationshipQuery(relationQuery).then(function(rslts){

                     var features = rslts[objectID].features;
                     features.forEach(function (ftr){
                       var t = ftr.attributes;
                       coreType = t.TYPE;
                       myArray.push(coreType);
                     });

                     function removeDuplicates(myArray){
                       let unique_array = []
                       for(let i = 0;i < myArray.length; i++){
                         if(unique_array.indexOf(myArray[i]) == -1){
                           unique_array.push(myArray[i])
                         }
                       }
                       return unique_array
                     }
                     console.log(myArray);
                     console.log(removeDuplicates(myArray));
                     finalArray = removeDuplicates(myArray);
                     //sampleTypes = "";
                     sampleTypes = finalArray.join(", ");
                     console.log(sampleTypes);
                  });

                  content += "<span class='bold' title='Types of sample collected'><b>Sample Types: </b></span>" + sampleTypes + "<br />";
                }
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Jay,

  Move line 40 inside your executeRelationshipQuery function handler (the "then" function).

0 Kudos
JayHill
Frequent Contributor

my console log?

0 Kudos
JayHill
Frequent Contributor

i added sampleTypes and still no results

queryTask.executeRelationshipQuery(relationQuery).then(function(rslts, sampleTypes){
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

  No that is not what I meant. Your code is asynchronous thus you have to wait for the realtionship query to return before you attempt to set the contents of your popup. The way your code in your original post is the popups content gets set using:

content += "<span class='bold' title='Types of sample collected'><b>Sample Types: </b></span>" + sampleTypes + "<br />";

Before the query ever completes. You you have to wait for the query to return before you set that line or show the popup.

0 Kudos
JayHill
Frequent Contributor

I see, thanks Robert!

0 Kudos