Reputation: 759
I'm using chrome storage in my app which was working fine. The process is very simple. User enters email and password, app verifies them using XHR request and maintain the session by using chrome.storage.local.set
. Once the session is set i reload the app using chrome.runtime.reload()
. On every reload i use checkLogin function to verify if the session is active or not. It was working fine. But now i'm getting this error:
It seems that the storage gets empty after reloading the app and the app cannot find the keyword in the storage and returning undefined error.
Here the code that is called once the user is authenticated:
postLogin = function (resultData) {
if (resultData.authStatus == 1) {
chrome.storage.local.set({'userData': resultData}, function(){
console.log("session saved");
});
chrome.runtime.reload()
}
else {
$("#result").html(resultData.authMsg);
$("#result").attr('style', 'display: inline-block !important');
}
return false
}
Once the app is reloaded i check the saved session:
checkLogin = function () {
console.log(chrome.storage);
chrome.storage.local.get("userData",function(result) {
userData = result.userData;
if (userData && userData.authStatus == 1) {
console.log("--- user signed In ---")
return true;
}
console.log(" ---- unable to find user session ----")
$("#auth-dialog").attr('style', 'display: inline !important');
return false;
})
}
At this point i'm getting the error i attached in the screenshot above. I also used console.log(chrome.storage);
to verify storage status and it returned undefined.
The weird thing is once i relaunch the app manually the .get
part of the code works fine. At first, it shows error i had to reload the page in order to get the stored storage value.
Manifest:
{
...
"permissions": [ "unlimitedStorage", "clipboardRead", "clipboardWrite", "nativeMessaging" , "storage" ],
}
UPDATE:
The issue is not with the chrome.storage
as mentioned in the post heading. The main issue with chrome.runtime.reload()
. After initial app launch, whenever i call chrome.runtime.reload()
as soon as the app reloads anything starting from chrome.
doesn't work.
Upvotes: 3
Views: 1218
Reputation: 759
I haven't found a solution but i've found a workaround for it. My original assumption was that after reloading the app chrome.storage
doesn't work because that's the first thing my app check. After some more debugging i found out that whenever i call chrome.runtime.reload()
as soon as the app reloads anything starting from chrome.
doesn't work. The issue is with the reloading the app To test this i've removed everything from the app just a log message showing manifest details console.log(chrome.runtime.getManifest())
and one button "Reload" on click event it calls chrome.runtime.reload()
. At initial app launch, everything works fine with a console log message showing manifest details. When i click Reload button and the app reloads it shows:
Uncaught TypeError: chrome.runtime.getManifest() is not a function
I tried using other functions like chrome.storage.local.set/get
with the same undefined error.
To resolve this, the workaround is to create a new window instead of reloading the app and close the previous one.
chrome.app.window.create(
'main.html',
opt);
setTimeout(function () {
//close previous window
}, 300);
If anyone having the same issue and found a better/cleaner workaround for this, please share.
Upvotes: 2