I'm new to React-Native and I was confused on how to have a variable that can be accessed by all the functions within a file without being in a class.
My issues is that I assigned storage in taskFour() and I want that value to be returned in runDemo() but for some reason when I console.log(storage) in runDemo() it returns undefined!
I have defined a helper file with a bunch of functions that call upon each other.
Helper.js
import React from 'react';
import SQLite from 'react-native-sqlite-storage';
let db;
let storage;
function runDemo() {
loadAndQueryDB();
//Suppose to return value assigned in queryPeopleSuccess but console logs 'undefined'
console.log(storage);
return storage;
}
// Sends an update saying that Database was successfully opened
function openCB() {
console.log("Success Opening DB");
}
// Sends an update with error message and returns FALSE
function errorCB(err) {
console.log("SQL Error: ", err);
return false;
}
/** 2. Called when runDemo is called **/
/* assigns variable 'db' to opened Database */
/* Calls queryPeople(db) */
function loadAndQueryDB() {
console.log("Opening Database...: ");
db = SQLite.openDatabase({ name: "users.db", createFromLocation: 1}, openCB, errorCB);
queryPeople(db);
}
/** 3. Called when loadAndQueryDB is called **/
/* Get the DB and applies a SQL call that if successful will call queryPeopleSuccess*/
function queryPeople(db) {
console.log("Executing employee query...");
//Execute a database transaction.
db.transaction((tx) => {
tx.executeSql('SELECT * FROM users', [], queryPeopleSuccess, errorCB);
});
}
function queryPeopleSuccess(tx, results) {
var len = results.rows.length;
let localArray = [];
//Go through each item in dataset
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
localArray.push(row);
}
storage = localArray;
}
export {
runDemo,
}
I thought by assigning "storage" outside of the functions would make it a variable accessible to all the functions in this file without making it a global variable. In addition, one restriction I have is that I can't return storage from queryPeopleSuccess all the way back to runDemo due to functions within these functions that are not suppose to have a return value!
Could someone please point out how I can have a variable within a file that does not need to be in a class that can be accessed and edited by the functions within that file?
EDITED: Edited for clarity and typos. Turns out the issue with my code is due to async!