JavaScript - Firebase value to global variable

Asked
Active3 hr before
Viewed126 times

4 Answers

javascriptfirebasevalue
90%

Storing the value in a global variable is not a problem, it's the when that is messing you up.,That is probably not the order you expected. But it explains perfectly why the global variable is not set when you access it: the value hasn't come back from the database yet.,Firebase return output from function,Data is loaded from Firebase asynchronously. This means that code doesn't run in the order that you probably expected. You can most easily see this by putting some logging in there:

Data is loaded from Firebase asynchronously. This means that code doesn't run in the order that you probably expected. You can most easily see this by putting some logging in there:

console.log("Before starting to load value");
ref.child("path").on("value", snapshot => {
   console.log("Loaded value");
});
console.log("After starting to load value");

That's why you'll want to move the code that needs the data from the database inside the callback function. Alternatively you can use the fact that once() returns a promise, which makes it easier to deal with as a result:

function loadData() {
   return ref.child("path").once("value");
});

loadData().then((snapshot) => {
   ...use the data from snapshot
});
load more v
88%

Hi! How can I save a global variable with "querySnapshot.size;" as the value inside the code snippet below? The code snippet I am talking about is this:,Then get a document Reference object of the likes doc for that collection and when you run the like function just update that document.,Hmmmm, if I understand what you are trying to do, I would have a doc in that collection with the ID set as "likes" with a single field and value set like


db.collection(parsed[0]).doc(parsed[1]).collection(parsed[2]).doc(parsed[3]).collection("likes").get().then(function(querySnapshot) {
   likecount = querySnapshot.size;
})
load more v
72%

foo = {};
foo[variable] = 'more stuff';
fb.set(foo);
65%

useValues is a function that returns an object containing current values of App Variables and Device Variables in your App.,useSetValue is a function that returns a setter function that, when called with an object containing key and value fields, will set an App Variable or a Device Variable with the specified key to the specified value.,In the App Variables menu, you can save API tokens to be used for any REST Services & Endpoints that you add to your project. The value for any App Variable is a string., App Variables Device Variables Configuration JSON Primitive Values GlobalVariableContext API Access in Custom Components useValues useSetValue Access in Custom Functions

import * as GlobalVariableContext from './config/GlobalVariableContext';
load more v

Other "javascript-firebase" queries related to "JavaScript - Firebase value to global variable"