This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
New to nodejs and really programming in general except some basic PHP. I think my problem is more basic javascript than anything though.
How can I access variables 'latitude' and 'longitude' outside the object 'img'?
console.log(latitude + '|' + longitude) is displaying the data I want but in console.log(img) all img properties are empty including exifData.gps.GPSLatitude & exifData.gps.GPSLongitude.
var ExifImage = require('exif').ExifImage;
var img_loc = 'c:/node/test/wherepic/uploaded_imgs/img.JPG';
var img = new ExifImage({ image : img_loc }, function (error, exifData) {
var latitude = exifData.gps.GPSLatitude;
var longitude = exifData.gps.GPSLongitude;
console.log(latitude + '|' + longitude);
});
console.log(img);
The problem is not the object, but the fact that your second console.log is executed before the data becomes available. Only when the function is called that data becomes available. You must access it inside the function.
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
Hi I can't get the value of a specific index of an array. I don't know why because if I try to log the entire array it works but with a specific index it doesn't work
This is my code, I use cloud firestore to get the ID of a document and save it into array
var idUsers = [];
const users_list = document.querySelector("#users_list");
db.collection("utenti").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
users_list.innerHTML += "<a href='utente.html' class='collection-item black-text'>" + doc.data().nome + " " + doc.data().cognome + " " + " </a>";
idUsers.push(doc.id);
});
});
console.log(idUsers);
console.log(idUsers[0]);
This is the result in Chrome console
enter image description here
Although it is considered to be a confusing statement, I assume that the operation is running asynchronously. Try to put a delay and then find value of idUsers[0] in order to find out the assumption.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am making a shopping cart via javascript/jQuery and I am using localStorage. However, I am running into a problem setting the key value of an object. I created this function, but it has not been successful:
function update_aantal(e){
var value = $(e).parent().find(".form-aantal-val");
var size = $(e).closest('td').attr('class');
console.log(size);
valuepush = {
size: value.val()
};
var cart = JSON.parse(localStorage.getItem("src"));
$.extend(cart[0], valuepuch );
localStorage.setItem("src",JSON.stringify(cart));
}
The console.log(size) shows the correct value that I want to change/add into the localStorage but the size in the object valuepush says "size" when the function is complete and the value is added.
How can I pass the size as an object so that the value of size is stored?
var valuepush = {};
valuepush[size] = value.val();
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I have a query created dynamically like,
"$select=" + selectedField + "&$orderby=" + selectedField + ""
This is just the dynamic part of the query where 'selectedField' is a string.
Now when I am getting my results (array of objects) and I am trying to get the values within a loop like,
for(var i=0; i<results.length; i++) {
var opt = results[i].selectedField;
}
opt is undefined. However, if the value of selectedField is 'new_name' and I write
var opt = results[i].new_name;
I get the desired result. Can anyone help me to resolve this?
Use var opt = results[i][selectedField];.
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 8 years ago.
This may be a question without any R&D but I am on a busy schedule and very new to these callback functions, The problem is I am getting a json payload from a webapp and I am trying to parse it , so this payload has a array of objects , but when i use this in my script i am getting only the last array index value.
below is the code and attached console output for reference , please suggest where i am going wrong
var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
var c1 = case1[i];
c1.retrieveAttributes(function(){
console.log(i+ " i");
console.dir(c1.attributes);
});
}
The i value in console is always 6.
You're having an issue with 'closure' scope. Try passing 'i' into your function like this.
var case1 = payload.Case;
var i=0;
for(i=0;i<case1.length;i++)
{
var c1 = case1[i];
c1.retrieveAttributes(function(i){
console.log(i+ " i");
console.dir(c1.attributes);
});
}
That should preserve the true value of 'i'.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 9 years ago.
Hi i am new to json and have some problems in parsing a JSON file with jQuery.
json file is here - http://maps.googleapis.com/maps/api/geocode/json?latlng=27.88,78.08&sensor=false
and i am parsing it like this
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";
$.getJSON(url, function(data) {
console.log("insidegetjson");
console.log(data);
var addr = data.results[0].formatted_address[0];
});
here i want to access first "formatted_address" part of the JSON. I know i am making a mistake here (i want "formatted_address" : "Achal Taal, Aligarh, Uttar Pradesh 202001, India",)
var addr = data.results[0].formatted_address[0];
can you please replace this sentence with correct sentence...thanks
Remove the last [0] :
var addr = data.results[0].formatted_address;
data.results[0].formatted_address isn't an array but a string.