javascript boolean variable's value is not changing [duplicate] - javascript

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 3 years ago.
I have a variable called itemExist and set that equal to false. My goal is to set this variable's value to true if a condition happens. My code is this:
var itemExist = false;
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>{
items.shoppingCart.forEach(async item=>{
if(item.productId == productId){
itemExist = true;
When I console log the itemExist variable outside of all theese functions
console.log(itemExist);
I get that result on the console:
false
But it should be true. How can I solve this problem, what is causing that?

Perform your work inside exec function
user.findById({_id: decoded._id}).populate('shoppingCart').exec((err, items)=>
{
const itemInArr = items.shoppingCart.find(item => item.productId === productId);
//!! - convertation to boolead value
itemExist = !!itemInArr;
console.log(itemExist);
}
);

Related

JavaScript and geolocation [duplicate]

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 1 year ago.
)
So I'm trying to learn about JavaScript and geolocation, but I've run into this, probably simple, problem:
let myLat; // Global
navigator.geolocation.getCurrentPosition((position) => {
myLat = position.coords.latitude;
console.log(myLat); // works
});
console.log(myLat); // undefined
I don't get why I cannot just use a global variable to hold the latitude-info?

jQuery - accessing variable outside onchange event? [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'm trying to make my code dependent on certain variables by changing them with a select. Is it possible to access the result outside of the function in jQuery?
js
function myCalendar(){
//global variable
var thismonth = '';
//onchange get selected month
$(document).on('change','#select_month',function(){
thismonth = $(this).val();
});
//check if empty
if(thismonth !==''){//always return empty?
var mm = thismonth;
}else{
var mm = new Date().getMonth();
}
}

how to call a variable defined below it in Javascript [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 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.

Not able to modify value in javascript callback [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I am not able to modify the value of outputString variable inside callback.
var outputString;
client.get(key,function(err,value){
outputString = "key="+key+" value="+value ;
console.log(outputString);
})
console.log(outputString);
When I print the value of outputString, it says "undefined"
client.get() returns immediately, so outputString hasn't been set when the last console.log() is called. The callback function isn't called until sometime later.

How to set the variable with chrome.storage.local.get in chrome [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)
Closed 7 years ago.
var curline;
chrome.storage.local.get("value",function(item)
{
window.curline=item["value"];
});
alert(curline);
I want to set the curline with item["value"],this code is in theinject.js,thanks.
chrome.storage API is asynchronous. The callback is executed later, after you alert.
Which means you must alert the result in the callback you pass :
var curline;
chrome.storage.local.get("value",function(item)
{
window.curline=item["value"];
alert(curline);
// here you may use curline, or pass it as argument to other functions
});

Categories

Resources