JQuery Getting value undefined [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 years ago.
I have the following JQuery script:
var loaded_data = {};
function data_load(callback) {
$.getJSON('get_data.php', function (data) {
$.each(data, function (fieldName, fieldValue) {
loaded_data[fieldName] = fieldValue;
});
});
callback();
}
function add_data() {
alert(loaded_data["courses"]);
}
data_load(function () {
add_data();
});
The function data_load() working properly and loading data. But when I am trying to call add_data() function, I am getting Undefined in alert message.
If I am Typing loaded_data["courses"] on console on browser, I am getting desired output.
I don't know loaded_data["courses"] is working properly on Browser console but not on script.
Is There anything like alert() is executing before loading data from get_data.php?

Related

Node js async fs.fileRead variable parsing [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I have the whole code wrapped by an async function, i'm using puppeteer basically.
If i use a simple fs.fileRead function, save the result of data inside a var then the code can read that variable in the next async function, and from my understanding it can read it exactly because is async same as the fs.fileRead function.
The problem comes when I try to do another fs.fileRead, way before the above, but in the same tree level, and try to read the variable saved inside it, in a if (){ statement right after, outside the fs.fileRead, I tried using a function but doesn't work either, below the example:
var fullcsv, filecsv;
fs.readFile('myfile.csv', (err, data) => {
if (err) {throw err;}
fullcsv = data.toString();
if(fullcsv.includes(urldom)){
filecsv = 0;
}else{
filecsv = 1;
}
processFile(filecsv);
});
var dog;
function processFile(filecsv) {
dog= filecsv;
}
console.log(dog);
if (urllink != "" && processFile() == 1){
...
The console says console.log(dog) is undefined , and the if statement turns out to be false cause it goes directlye to the else statement.
p.s. I also tried with fs.fileReadSync but does literally no difference at all.

function that has an ajax events return undefined when called [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have a function called 'getAuthor' and inside there's an ajax events running (refer below)
function getAuthor(id){
$.get('http://www.connectnigeria.com/articles/wp-json/wp/v2/users/74',function(e){
var author = e.name;
console.log(author);
return author;
});
}
try to run from the console, you'll see it returns "undefined" while console.log display the expected response from the ajax call.
Any, ideas help please?
Unless there is a explicit return from a function , it will always return undefined.
In console it is undefined because the function getAuthor is not returning anything. Not to be confused with the return from $.get.
The console.log statement is inside this asynchronous function. To get a return from the function getAuthor try this
function getAuthor(id){
return $.get('http://www.connectnigeria.com/articles/wp-json/wp/v2/users/74',function(e){
var author = e.name;
console.log(author);
return author;
});
}

Javascript scope variable [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
var preGameRequest = new XMLHttpRequest();
var preGameData;
preGameRequest.open("GET", "/matches/live.json");
preGameRequest.onload = function() {
preGameData = JSON.parse(preGameRequest.responseText);
}
preGameRequest.send();
console.log(preGameData); // main problem is here
Here is my code. I defined preGameData as global and tried to save /matches/live.json file's data into preGameData. And when I try to console.log(preGameData) from outside of the scope (like in the code section) I get 'undefined' as a return. And if I try to console.log(preGameData) from inside of the scope it works. I don't really know what's going on.
That code should work:
preGameRequest.onload = function() {
preGameData = JSON.parse(preGameRequest.responseText);
console.log(preGameData);
}
The reason: is that preGameRequest.send() function operates asynchronous.
When the browser tries to execute console.log() (in your example), the http response is not recieved yet, and the variable is really undefined.
But if you place console.log() inside of the handler (as in my example), it would be executed at moment, when the response is already recieved (straight in the onload() handler). And that's why the variable preGameData would be defined already.

Read variable inside function in Javascript? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
Right now I have three files, two javascript files and one html file that will call upon these two javascript files. Inside my first javascript file, let's call it part1.js, I have this content:
var dataval;
$.get('example.cgi', function(data){
dataval = data;
});
Inside part2.js, I am trying to call on this dataval value. But it is not working, as I am assuming that dataval is inside the get function and is therefore a local value. Is there anyway I can grab that value? My html file is calling on both of these javascripts. Is there anyway I can access that variable?
Thanks!!
There is a problem here with asynchronous calls. You should either use a callback function or make use of the jquery promise capability.
In your part1.js, you should instead define a function:
function callCgi(callback) {
$.get('example.cgi', callback); //Callback will receive the data.
}
and then use it in part2.js:
callCgi(function(data) {
//do something with data.
});
Or with promises:
function callCgi() {
return $.get('example.cgi');
}
and then use it in part2.js:
callCgi().then(function(data) {
//do something with data.
});

how can i get return value of javascript function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How to return AJAX response Text? [duplicate]
(2 answers)
Closed 8 years ago.
my code like this
var key,value;
//……some code……
function checkReg(i,j){
$.getJSON(api_checkExists,{"k":i,"v":j,"rand":Math.random()},function(c){
alert(c.status);
return(c.status);
});
}
var temp=checkReg(key,value);
alert(temp);
The problem is when I run it in my browser, it always fist alert undefined then alert the return value; seems like that can not get the return value of function checkReg. I was so confused, what should I do?
That's because your ajax call is running asynch from the rest. Your code will already have returned before before the ajax is even triggered.
So the returning doesn't work like that, you should provide a callback to your function instead of trying to return it.
function checkReg(i,j, callback){
$.getJSON(api_checkExists,{"k":i,"v":j,"rand":Math.random()},function(c){
alert(c.status);
callback(c.status);
});
}
checkReg(key,value, function(temp){
alert(temp);
});
you cannot do that... since ajax is ashynchronus ... by the time the success function is called and return, the alert will be called thus having temp as undefined or empty.. one way to do this is doing the thing you need to inside the success called back of getJSON... or use $.deffered or callinga calback function..
function checkReg(i,j, callback){
$.getJSON(api_checkExists,{"k":i,"v":j,"rand":Math.random()},function(c){
callback(c.status);
});
}
checkReg(key,value, function(temp){
alert(temp);
});

Categories

Resources