This question already has answers here:
How do I return the response from an Observable/http/async call in angular?
(10 answers)
Closed 5 years ago.
var relative = af.database.object('users/user75ECZOiNtxZwYoezaXmYA9YwPm53', { preserveSnapshot: true });
relative.subscribe(
snapshot => {
this.usedBasicProfile = snapshot;
}
);
console.log(this.usedBasicProfile); //the value is undefined, how to make this work?
what I want to do is get the data from the firebase, anyone could help me?
It appears you're using an async method which means the value is not available until execution gets inside the callback handler. Have you tried this?
var relative = af.database.object('users/user75ECZOiNtxZwYoezaXmYA9YwPm53', { preserveSnapshot: true });
relative.subscribe(snapshot => {
this.usedBasicProfile = snapshot;
console.log(this.usedBasicProfile);
});
Related
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 am working with angular and typescript and want to change the value of the variable inside the function. I am using a service to get the data. The code is shown below:
isValue:boolean = false;
onChangeValue(list: List) {
this.someServive.getData(list).subscribe(
item => {
if(item.value === 1) {
this.isValue = true;
}
});
console.log(this.isValue); //the value is still false here
}
Good day!
It happens due to the fact, that your service call is an async operation. And browser doesn't wait when your service emits some value, which you handle in subscription block.
isValue = false;
onChangeValue(list: List) {
this.someServive.getData(list).subscribe(item => {
if (item.value === 1) {
this.isValue = true;
this.executeOnUpdateCallback();
}
});
}
executeOnUpdateCallback() {
console.log(this.isValue); // true
}
Something like this you should do to handle it. I hope, you got it (please google about js event loop)
Since javascript is asynchronous, the value won't change until the getData service call actually happens.
In your case console executes before the getData happens.
You can either try it with promise or callback
Try below:
isValue:boolean = false;
onChangeValue(list: List) {
onDataRecieved((res)=>{
console.log(this.isValue);
});
}
onDataRecieved(cb){
this.someServive.getData(list).subscribe(
item => {
if(item.value === 1) {
this.isValue = true;
cb(true);
}
});
}
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 4 years ago.
let positionSettings = require('setting');
function getSetting(element) {
let setting;
positionSettings.find({element: element}, function (err,docs) {
console.log(docs[0].current); // output the value
setting = docs[0].current;
});
return setting;
}
$(document).on("click", "#save" , function(e) {
console.log(getSetting("abc")); // Output undefined
});
Why is the getSetting() function returning undefined. How can I achieve it.
Because it's an async function, you can't mix async and sync functions this way, try something like this:
let positionSettings = require('setting');
function getSetting(element, callback) {
positionSettings.find({element: element}, (err,docs) => {
let setting;
console.log(docs[0].current); // output the value
setting = docs[0].current;
callback(setting);
});
}
$(document).on("click", "#save" , (e) => {
getSetting("abc", (setting) => {
console.log(setting);
});
});
The fact is that you can't be sure that the data will be avaible after the function call, the only place that you have this sure is inside the callback.
And just a hint, use arrow functions to declare anonymous functions.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
The following Node.js code prints 'undefined', even though it found the file.
var fileFound = function() {
fs.readFile('public/images/acphotos/Friedrich-EL36N35B.jpg', function(err, data) {
if (err) {
console.log(err);
return false;
} else {
return true;
}
});
}
console.log("Return value: " + fileFound());
How would I rewrite it? I don't fully understand the solution in the other thread I was shown.
Because the return statements are inside the callback passed into fs.readFile.
the fileFound function never returns anything, therefore you get undefined.
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 7 years ago.
I have had a look, and see some great examples of nesting functions, but even with reading them, I cannot see why I am getting undefined when i call this function:
function readBttnDetec() {
var devid = localStorage.getItem('vBttn');
var bd = 0;
bd = ble.read(devid, 'fffffff0-00f7-4000-b000-000000000000',
'FFFFFFF2-00F7-4000-B000-000000000000',
function(t) {
var data = new Uint8Array(t)
console.log('returns: ' + data[0]); // this returns 6
return data[0];
}, function(f) {
console.log(f);
});
return bd;
}
This is the call:
//check button state
var detecs = readBttnDetec();
console.log(detecs);
if(detecs == 2) {
// fall detection disabled
$('#playfall').removeClass('km-state-active');
} else if(detecs == 6) {
// fall detection enabled
$('#playfall').addClass('km-state-active');
} else {
// error reading button
}
I am missing something simple I am sure of it, but I cannot see it.
Thanks in advance
This question already has answers here:
Wait for all promises to resolve
(5 answers)
Closed 8 years ago.
Suppose I have 2 or more async calls:
$scope.obja = {};
$scope.objb = {};
ref1.on('value', function (value) {
$scope.obja= value.val();
});
ref2.on('value', function (value) {
$scope.objb= value.val();
});
Later within the script I want to make sure that both of the $scope variables are set before I use them.
This example code follows the firebase examples in angularjs.
Any ideas?
Surely you can just check with an if statement?
if (($scope.obja != {}) && ($scope.objb != {})) {
// they have been set
}
Or, if they stand a chance of getting set to an empty object, just have a second variable that gets set with them:
var setn = 0;
// ...
ref1.on('value', function (value) {
$scope.obja= value.val();
setn++;
});
ref2.on('value', function (value) {
$scope.objb= value.val();
setn++;
});
// ...
// later in code, check if they have both been set
if (setn == 2) {
// they have both been set
}