This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why Does Firebase Lose Reference outside the once() Function?
(2 answers)
returning array after finish reading from Firebase
(1 answer)
Closed 1 year ago.
I guess this is not a Firebase question as much as it is a callback syntax question:
I want the function changeName to go to Firebase database and replace the value of "t" with a value that is a child of "t" in the database.
function changeName (t) {
return (
t.replace(t, firebase.database().ref(`langs/${t}`).once('value', snap => snap.val().name))
)
}
But I get [object Promise].
The code does work when I put an alert() method inside the snap arrow function, so I know the access to the value is fine. I just couldn't figure out how to get the value as a string of text for the replace() method.
This should work
function changeName (t) {
return (
firebase.database().ref(`langs/${t}`).once('value', snap => {
t.replace(t, snap.val().name);
})
)
}
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)
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.
This question already has answers here:
Why doesn't my arrow function return a value?
(1 answer)
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I want to construct an object using map. I am firing sql query inside of map function, but when i send the final map array it is giving null value.
var result = rows.map((uod) => {
UserOrder.findOne("id", uod.userorderid).then((userorder) => {
uod.userorder = userorder
return uod
})
})
res.send(result)
I want the function to return the main object with "userorder" object as key value pair. But it is returning [null,null....]
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I have a function, which contains a sub function. I need to return a variable from the sub function however I am not sure how (I am new to JS).
I also want to avoid using a global variable, as I have about 15 more functions similar to this.
It shows the error, x is not defined.
return data.rr_vin
^
ReferenceError: data is not defined
I have tried declaring the variable as an attribute of the function, i.e. function.myvar, however I still get the same issue.
function find_num() {
fetch("somefile.json")
.then(res => res.json())
.then(data => {
a = data.numdata.filter(el => el.Process==="Process1").map(o => o.Finish_Time);
b = recent_time(a)
data.x= Math.max(data.numdata.filter(el => el.Finish_Time===b).map(o => o.Shortnum));
console.log(data.x)
return data.x
})
return data.x
}
Ideally I want to call on the find_num() function to return x.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This may sound a newbie question but I'm having really hard time with variable scopes in Javascript.
I have the following JS snippet:
<script>
window.IDFVPlugin.getIdentifier(function(result){ uuid = result; });
alert(uuid);
</script>
I want to use the variable uuid anywhere in the script outside the window object. uuid returns the correct value only when inside the object and the value is lost when outside. So the alert above will log an undefined variable error.
You use a callback function. Result should be used inside of callback body. If you try to use it immediately after main function call - it will not be yet available
window.IDFVPlugin.getIdentifier(function(result){
uuid = result;
alert(uuid);
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
There's probably an obvious mistake somewhere, but I just can't out find what's wrong.
I'm trying to retrieve data from an api, and use the js-function below to get it. If I do an alert(key) on the data inside the $.get-function, it alerts the correct data.
function getApiKey(company, password) {
var url = "http://myapi.com/" +company+ "?password=" +password;
var key = "";
$.get(url).done(function(data) {
key = data;
//alert(key) returns the correct data
});
return key;
}
However, I need to use the function in a different file, and that's where it doesn't work. When I do
var key = getApiKey("company", "password");
alert(key);
key is empty.
The $.get command is asynchronous, meaning that your function returns key with its initial value of "", then later when the async callback fires, it runs key=data.
You should read up on asynchronous behaviour in javascript - for a solution, you'll need some way to get the data back to the place of the call asynchronously. One such option is a promise.