Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:04:54+00:00 2026-06-02T02:04:54+00:00

basic JS question, please go easy on me I’m a newb :) I pass

  • 0

basic JS question, please go easy on me I’m a newb 🙂

I pass 2 variables to the findRelatedRecords function which queries other related tables and assembles an Array of Objects, called data. Since findRelatedRecords has so many inner functions, I’m having a hard time getting the data Array out of the function.

As it currently is, I call showWin inside findRelatedRecords, but I’d like to change it so that I can get data Array directly out of findRelatedRecords, and not jump to showWin

function findRelatedRecords(features,evtObj){
    //first relationship query to find related branches

    var selFeat = features
    var featObjId = selFeat[0].attributes.OBJECTID_1        
    var relatedBranch = new esri.tasks.RelationshipQuery();
    relatedBranch.outFields = ["*"];
    relatedBranch.relationshipId = 1; //fac -to- Branch
    relatedBranch.objectIds = [featObjId];
        facSel.queryRelatedFeatures(relatedBranch, function(relatedBranches) {
        var branchFound = false;
        if(relatedBranches.hasOwnProperty(featObjId) == true){
            branchFound = true;
            var branchSet = relatedBranches[featObjId]
            var cmdBranch = dojo.map(branchSet.features, function(feature){
                return feature.attributes;
            })
        }

        //regardless of whether a branch is found or not, we have to run the cmdMain relationship query
        //the parent is still fac, no advantage of the parent being branch since cmcMain query has to be run regardless
        //fac - branch - cmdMain - cmdSub <--sometimes
        //fac - cmdMain - cmdSub <-- sometimes

        //second relationship query to find related cmdMains            
        var relatedQuery = new esri.tasks.RelationshipQuery();
        relatedQuery.outFields = ["*"];
        relatedQuery.relationshipId = 0; //fac -to- cmdMain
        relatedQuery.objectIds = [featObjId];
        //rather then listen for "OnSelectionComplete" we are using the queryRelatedFeatures callback function
        facSel.queryRelatedFeatures(relatedQuery, function(relatedRecords) {
            var data = []           
            //if any cmdMain records were found, relatedRecords object will have a property = to the OBJECTID of the clicked feature
            //i.e. if cmdMain records are found, true will be returned; and continue with finding cmdSub records
            if(relatedRecords.hasOwnProperty(featObjId) == true){
                var fset = relatedRecords[featObjId]
                var cmdMain = dojo.map(fset.features, function(feature) {
                    return feature.attributes;
                })
                //we need to fill an array with the objectids of the returned cmdMain records
                //the length of this list == total number of mainCmd records returned for the clicked facility
                objs = []
                for (var k in cmdMain){
                    var o = cmdMain[k];
                    objs.push(o.OBJECTID)                       
                }

                //third relationship query to find records related to cmdMain (cmdSub)
                var subQuery = new esri.tasks.RelationshipQuery();
                subQuery.outFields = ["*"];
                subQuery.relationshipId = 2;
                subQuery.objectIds = [objs]
                subTbl.queryRelatedFeatures(subQuery, function (subRecords){

                    //subRecords is an object where each property is the objectid of a cmdMain record
                    //if a cmdRecord objectid is present in subRecords property, cmdMain has sub records
                    //we no longer need these objectids, so we'll remove them and put the array into cmdsub
                    var cmdSub = []
                    for (id in subRecords){
                        dojo.forEach(subRecords[id].features, function(rec){
                            cmdSub.push(rec.attributes)
                        })
                    }
                    var j = cmdSub.length;
                    var p;
                    var sub_key;
                    var obj;
                    if (branchFound == true){
                        var p1 = "branch";
                        obj1 = {};
                        obj1[p1] = [cmdBranch[0].Branches]
                        data.push(obj1)
                    }                        
                    for (var i=0, iLen = cmdMain.length; i<iLen; i++) {
                        p = cmdMain[i].ASGMT_Name
                        obj = {};
                        obj[p] = [];
                        sub_key = cmdMain[i].sub_key;
                        for (var j=0, jLen=cmdSub.length; j<jLen; j++) {
                            if (cmdSub[j].sub_key == sub_key) {
                                obj[p].push(cmdSub[j].Long_Name);
                            }
                        }
                        data.push(obj);
                    }
                    showWin(data,evtObj) <---this would go away
                })
            }
            //no returned cmdRecords; cmdData not available
            else{
                p = "No Data Available"
                obj = {}
                obj[p] = []
                data.push(obj)
            }
            showWin(data,evtObj) <--this would go away
        })
    })
}

I’d like to have access to data array simply by calling

function findRelatedRecords(feature,evt){
    //code pasted above
}

function newfunct(){
    var newData = findRelatedRecords(feature,evt)
    console.log(newData)
}

is this possible?

thanks!

Edit

Little more explanation…..

I’m connecting an Object event Listener to a Function like so:

function b (input){
    dojo.connect(obj, "onQueryRelatedFeaturesComplete", getData);
    obj.queryRelatedFeatures(input);
    console.log(arr) //<----this doesn't work
}

function getData(relatedFeatData){
    var arr = [];
    //populate arr
    return arr;
}

So when obj.QueryRelatedFeatures() is complete, getData fires; this part works fine, but how to I access arr from function b ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:04:56+00:00Added an answer on June 2, 2026 at 2:04 am

    Post Edit Update:

    Due to the way that this event is being hooked up you can’t simple return data from it. Returning will just let Dojo call to the next method that is hooked up to onSelectionComplete.

    When init runs it is long before findRelatedRecords will ever be executed/fired by the onSelectionComplete event of the well, which is why you were seeing undefined/null values. The only way to work with this sort of system is to either 1) call off to a method like you’re already doing or 2) fire off a custom event/message (technically it’s still just calling off to a method).

    If you want to make this method easier to work with you should refactor/extract snippets of it to make it a smaller function but contained in many functions. Also, changing it to have only one exit point at the end of the findRelatedRecords method will help. The function defined inside of subTbl.queryRelatedFeatures() would be a great place to start.

    Sorry, you’re kind of limited by what Dojo gives you in this case.

    Pre Edit Answer:

    Just return your data out of it. Everywhere where there is a showWin call just use this return.

      return {
        data: data,
        evtObj: evtObj
      }
    

    Then your newfunct would look like this.

    function newfunct(){
        var newData = findRelatedRecords(feature,evt);
        console.log(newData);
        console.log(newData.data);
        console.log(newData.evtObj);
    }
    

    If you only need that “data” object, then change your return to just return data;.

    Also, start using semicolons to terminate statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very basic question, so please bear with me. Consider the following
I'm new to seo, so please excuse what may be a very basic question.
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+
Perhaps this is a very basic question, please pardon me if it is ---
First off, please accept my apologies if this question is basic, I mainly have
I'm very new to both of these so please forgive my basic question. I
I am a newbie. please excuse me if it is a very basic question.
Please forgive this rather basic question, but I'm very new to Java and still
Probably a very basic question - I have 2 tables #favorites and #leaders ,
A basic question please, I am trying this code: var number_questions = postsJSON1[i]['question'].length; for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.