0

I am working on an developing a data source object (called data variable in my program) based on Solr queries using require.js

My goal is to pass the data variable to an API that I am developing, hence the data variable needs to be accessible outside the require.js block as well but it isn't. To solve the problem, I came up with 2 dummy data variables called data1 and data2 to pass the values but still I am not able to access any of these 2 variables outside the require.js block or you can say outside the function (response)

Here is my JS code:

var data2 = null;

require(['jquery'], function($) {
  var solrServiceURL = new XWiki.Document('SuggestSolrService', 'XWiki').getURL('get');
  var data1 = null;
  $.post(solrServiceURL, {
    outputSyntax: 'plain',
    nb: 100,
    media: 'json',
    query: [
      'q=*:*',
      'q.op=AND',
      'fq=type:DOCUMENT',
      'fl=reference'
    ].join('\n'),
    input: " "
  }, function(response) {
    data1 = response;
    data2 = data1;
    console.log("This is data1 but inside the function of require block:", data1); // No error here
  });
});


console.log("This is the array of data1 outside any function:", data1); // Error
console.log("This is the array of data2 outside any function:", data2); // Error

The error I get is:

Uncaught ReferenceError: data1 is not defined

How do I access the variable outside any function so that I can pass this data object to my API which I am developing? At the moment, I cannot pass this object anywhere! Thanks in advance for the help!

EDIT: If I declare both variables globally as pointed in the first comment, I get the values of data variables as null in the console. Still no help. Here is the console output:

This is the array of data1 outside any function: null
This is the array of data2 outside any function: null

What I did was to declare both variables globally:

var data2 = null;
var data1 = null;

and

function(response) {
    data1 = response;
    data2 = data1;
}
wthrajat
  • 32
  • 6
  • that's the same with any variable declared inside a function ... it's scope means it's not accessible outside the function. Nothing at all to do with "requirejs" - declare data1 like you do data2 ... then don't use `var data1` just `data1 = ...etc` inside that function and et voila – Jaromanda X Jun 10 '23 at 08:10
  • @JaromandaX I tried this also and didn't get any error but: `This is the array of data1 outside any function: null` `This is the array of data2 outside any function: null` What I did was to declare both variables globally: `var data2 = null;` `var data1 = null;` and `function(response) { data1 = response; data2 = data1; }` – wthrajat Jun 10 '23 at 08:25
  • yes, you did, and found it worked? I don't know what you're saying – Jaromanda X Jun 10 '23 at 08:27
  • @JaromandaX Now I get the value of those 2 variables as `null` outside the function. I've edited my post, please see the last section of the post. – wthrajat Jun 10 '23 at 08:31
  • that's because `$.post` is *asynchronous* and now you're console.log is running **before** `function(response) {` is called – Jaromanda X Jun 10 '23 at 08:40
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jabaa Jun 10 '23 at 11:27
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – jabaa Jun 10 '23 at 11:30
  • Instead, call a function (or do what you want to do with those values) from inside your callback (where you try to assign `data1` and `data2` now). – MatsLindh Jun 10 '23 at 13:28

0 Answers0