how to call a variable defined below it in Javascript [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
i have the following code and i'm trying to console log a variable called trt i created below it: how can i get trt in console log??
thanks
this.rows = this.dataService.SetItem('added-items',());{
console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
//this.rows = this.dataService.SetItem("data",this.rows);
})
thanks!!

You need to log it to the console after you define it.
this.rows = this.dataService.SetItem('added-items',());{
//console.log(trt);
this.dataService.GetJson().subscribe((result)=>
{
this.rows=(JSON.parse(result['_body']));
var trt=(result['_body']);
console.log(trt);
//this.rows = this.dataService.SetItem("data",this.rows);
})
Edit: you cannot call a variable that has not been defined yet and expect it to not be undefined.

Related

JavaScript - Problem getting data from promise object [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
So, I'm new to using promise object... I'm trying to make a Facebook instant game which uses the function FBInstant.player.getDataAsync().then(). This function returns a promise and I have no clue how to use the data returned other than with console.log(). If I try to insert the data to a defined variable it becomes unidentified.
Example:
User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
User['name'] = data['name'];
});
console.log(User['name']) // return unidentified.
Sorry for my poor explanation I'm new to this and I'm just trying to accomplish a simple task.
User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
User['name'] = data['name'];
console.log(User['name'])
});
This should give you a response

Object has contents, as long as I don't want to specifically adress them [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)
Console.log showing only the updated version of the object printed
(3 answers)
Closed 2 years ago.
I have this React project, in which I fetch data from a firebase database and construct a object called "statehelp" from the results. Now for some reason, if I console log the whole statehelp Object I get all its contents, however if I want to access statehelp.contents or statehelp.products they are undefined.
componentDidMount() {
var statehelp = {
content: {},
products: {}
}
getFarm(this.id).then(result => statehelp.content = result)
getProduct(this.id).then(result => statehelp.products = result)
console.log(statehelp)
console.log(statehelp.content)
}
That happens because console.log() is synchronous operation. It won't wait for your api call to happen. So don't use console.log() as a metric for identifying whether your object got populated or not.

Javascript json response undefined [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 5 years ago.
I have function like this:
function GetLala() {
var lala = $.getJSON(uriLala).done(function (data) {
return data;
});
return lala;
}
Then code like this:
var data = GetLala();
console.log(data.responseJSON);
This returned me undefined. If I type this code in google chrome console - then work.

global variable in javascript not working; loses data [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
var feed;
$.getJSON('short.json', function(data) {
feed = data.items;
console.log(feed);
});
console.log(feed);
I have this short code written above. I am expecting feed to be a global variable but once it comes out the function, it's undefined again. It prints out an object when inside. What am I doing wrong?
Thanks for the help.
The reason is that the getJSON() call is asynchronous. It won't run until AFTER the second console.log();

why do the individual parts of this function work on their own but the function as a whole fails? [duplicate]

This question already has answers here:
jquery ajax get responsetext from http url
(10 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
If I copy and paste each part of this function everything works. However when I try to use the function on its own I get:
VM7192:4 Uncaught TypeError: Cannot read property 'match' of undefined
My function is:
var read_f = function() {
var restext = $.get('file.txt');
var text = restext.responseText;
text = text.match(/[a-zA-Z0-9]/g);
text = text.join('').split(/Q0/);
return text;
};
What's happening ?

Categories

Resources