I have made a UserContext.js which handles getting and setting the current logged in user for the app. It has two function; one takes a JSON object and writes it with Storage.writeSync() and the other gets the previously stored user with Storage.readSync() and returns it.
UserContext.js:
var Observable = require('FuseJS/Observable');
var Storage = require("FuseJS/Storage");
function getUser() {
return new Promise(function(resolve, reject) {
var contents = Storage.readSync("User.json");
var user = JSON.parse(contents);
resolve(user);
});
}
function setUser(userObject) {
return new Promise(function(resolve, reject) {
var success = Storage.writeSync("User.json", JSON.stringify(userObject));
if(success) {
console.log("Successfully wrote to file");
}
else {
console.log("An error occured!");
}
resolve();
});
}
module.exports = {
getUser: getUser,
setUser: setUser
};
Now I have tested getting the JSON data with readSync() inside UserContext.js and print it and it works just fine.
However when I try to do
var User = UserContext.getUser();
console.log(User.firstname);
in LoginPage.js then it just returns null…
Anyone know why this happens and have a solution? The variable should be available throughout the app once it’s fetched and written.