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;
}