retrieve value from a JavaScript function

Just a simple JS question, please be patient with me as I'm new to this :)

I am passing 2 variables to the findRelatedRecords function which queries other related tables and constructs an Array of Objects called data. Due to the many inner functions in findRelatedRecords, I am struggling to retrieve the data Array from the function.

Currently, I call showWin inside findRelatedRecords, but my goal is to modify it so that I can directly extract the data Array out of findRelatedRecords without having to go through showWin.


function findRelatedRecords(features, evtObj){
    // Function code goes here...
}

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

Is there a way to achieve this?

Thank you!

Edit:

Additional explanation...

I am linking an Object event Listener to a Function like this:


function mainFunction(input){
    dojo.connect(object, "onQueryRelatedFeaturesComplete", getData);
    object.queryRelatedFeatures(input);
    console.log(array); // This doesn't work
}

function getData(relatedFeatData){
    var array = [];
    // Populate the array
    return array;
}

When object.QueryRelatedFeatures() is complete, getData is triggered; this part works well, but how do I access the 'array' from the mainFunction?

Answer №1

Update after Editing:

The process of this event hookup does not allow for a simple return of data. Returning the data will prompt Dojo to move on to the next method connected to onSelectionComplete.

At the time init is executed, it happens long before findRelatedRecords is triggered by the onSelectionComplete event. This could explain why you were encountering undefined/null values. To work within this system, you can either 1) call a method as you are currently doing or 2) trigger a custom event/message (which essentially involves calling a method).

To make this method more manageable, consider refactoring and breaking it down into smaller functions. It would be beneficial to have only one exit point at the end of the findRelatedRecords method. A good starting point would be the function defined inside subTbl.queryRelatedFeatures().

Unfortunately, your options are somewhat limited by the constraints imposed by Dojo in this scenario.

Prior Answer Before Editing:

To retrieve your data, simply include a return statement. Whenever there is a showWin call, use this return statement.

  return {
    data: data,
    evtObj: evtObj
  }

Your new function should resemble the following.

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

If you only require the "data" object, modify your return statement to just return data;.

Additionally, remember to use semicolons to end statements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting problems with dates in jQuery AJAX

Hey, I recently worked on formatting dates in jQuery Ajax. After fetching a date value from the database, I converted it to the format dd-MM-YYYY. However, there seems to be an issue where I'm receiving the previous month. For example, if the database ...

What is the best method for storing and accessing audio files that users upload in my MERN application?

I am developing a MERN application and am looking to implement a feature that allows users to upload audio files. I have read that storing the files directly in the database or within a folder inside the app is not recommended. I need a solution that en ...

Display alternative component upon button click in React.js

After creating the default layout, I'm aiming for a scenario where clicking each button only alters specific parts of the layout through the content component. React router is being utilized to manage different pages. Each button corresponds to a uni ...

Executing functions across modules in node.js

I need assistance with two of my modules: var client = require('./handlers/client.js'); var server = require('./handlers/server.js'); server.createClient() The client module: var client = function(){ console.log("New client"); ...

At what point can we rely on the accuracy and timeliness of Element.getBoundingClientRect?

I am currently in the process of developing some code that utilizes Element.getBoundingClientRect (gBCR), combined with inline style updates, to carry out calculations. This particular project is not intended for a general website, so I am not interested i ...

Mandate that users select from the available place autocomplete suggestions exclusively

I have integrated the "Places" Google API to enable address autocomplete in an input field on my website. However, since the service is limited to a specific area, I have implemented an autocomplete filter. The issue I'm facing is that users are stil ...

The hidden DIV containing an ASP.NET CheckBox consistently yields a value of false

I have a group of form elements located within a hidden div which looks like this: <div id="jDivUpdateFolder" style="display:none;"> <asp:TextBox ID="txtEditFolderName" runat="server"></asp:TextBox><br /> <asp:TextBox ID ...

Sending a JSON object as a parameter in JavaScript with whitespace within the data

It appears that the code is functional when there are no spaces in the content <a onclick=fbShareDialog("{\"name\":\"aaaaaaa\"}"> However, if there is a space present <a onclick=fbShareDialog("{\"name\":\"bbbb ...

Ways to render component solely based on the alteration of the class props

In my ReactJS project, I am fetching JSON data from a RESTful Django host and using it to create a table with filters. Here is my Table class: class MainTable extends React.Component { constructor(props) { super(props); this.state = { res ...

Ways to bypass browser pop-up blockers when using the window.open function

I am displaying an HTML retrieved from the backend. printHtml(htmlContent) { var windowToPrint = window.open('', '_blank'); windowToPrint.document.write(htmlContent); setTimeout(function () { windowToPrint.document ...

Do I have to specify the protocol when loading jQuery?

Underneath is the code snippet: <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script> <script type="text/javascript"> console.log(jQuery); </script> This code works perfectl ...

Conceal the div element when the contents are equal to zero

Below is the code snippet I am working with: <div class="col-md-6 col-xs-6 StockNumber"> In Stock: <span class="NumberOfStock"> <?php echo $this->product->product_in_stock ?> </span> </div> You can ...

Setting orientations for portrait and landscape views using Material UI breakpoints

Looking to implement portrait and landscape views for tablets using the styles object in Material UI. const tabletStyles = theme => ({ root: { padding: theme.spacing.unit, [theme.breakpoints.up('md')]: { backgroundColor: theme ...

Executing a function in the view/template with Angular 2+

Whenever a function is called in the view of an Angular component, it seems to be executed repeatedly. A typical example of this scenario can be seen below: nightclub.component.ts import { Component } from '@angular/core'; @Component({ selec ...

How to run .lnc files in Node.js

I encountered an issue while trying to open a shortcut (.lnk) file using node.js. What is the correct way to do this? var exec = require('child_process').execFile; var runLibreOffice =function(){ exec('D:\\Downloads\&b ...

Coffeescript does not allow setting the AngularJS controller property as the last line of code

Having an issue while using Coffeescript to define a controller with the "HomeController as homeCtrl" syntax. angular.module('myApp.controllers',[]).controller("HomeController", -> @someArray = [] # return ) Encountering a problem ...

Update article translation dynamically in Vue without needing to refresh the page

ArticleController public function index() { $locale = Lang::locale(); // $locale = Lang::getLocale(); $articles = Article::withTranslations($locale)->get(); return $articles; } resources/assets/js/pages/articl ...

What is the most effective method for transitioning between pages while incorporating eye-catching animation effects?

I'm currently working on a website and I'm looking to add some animated page transitions. Is there a method to achieve this without using ajax to call the next page and display it with all the required effects? Or should I consider using React to ...

Update the image "done.img" when the todoapp is clicked using VueJS

I am currently practicing with VueJs and I would like to be able to click on the 'awaiting.img' image, so that the 'done.img' image will appear instead of the other one. Currently, every time I click on the 'awaiting.img' imag ...

Crafting a clever smart banner using jquery.smartbanner for Android Firefox users

In order to display smartbanners on my mobile website, I utilize jquery.smartbanner. This implementation has been successful for ios, windows phone, and the default android browser. However, when testing on Firefox for android, the smartbanner is not visib ...