indexedDB.deleteDatabase() throws error - javascript

I am trying to delete a database for unit testing:
var DeleteDb = indexedDB.deleteDatabase(dbName);
this piece of code gets directly called in the first beforeEach in jasmine.
with chrome devTools i can see that DeleteDb.error has the following value:
error: [Exception: DOMException: Failed to read the 'error' property from 'IDBRequest': The request has not finished.]
DeleteDb.onsuccess or DeleteDb.onerror are never fired. Also i am never trying to read an 'error' property in the code.
Does somebody know what this error means, where it is coming from or how i can solve it?
UPDATE:
This code still sometimes fails for me. it gives me the same error as above.
<html>
<head>
<script>
var deleteDb = window.indexedDB.deleteDatabase('fakeNonExistentDatabase');
deleteDb.onsuccess = function() {
console.log('complete');
};
</script>
</head>
</html>
I am still not sure why this is happening. sometimes opening a new tab or closing another one works.
Also it never gives an error when removing the onsuccess callback
UPDATE 2
Well it seems that the code is actualy working, but the onsuccess event is never getting fired because no events are getting fired anymore when pressing F8 in chrome devtools. The error i;m getting now is:
Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.
I thought the above error message was a result of the first error message in this post but actualy it was the other way around. There are a few pages i can find about this topic on the internet but there isn't really provided a answer.
Any thoughts?

Also check onblocked. There might be another db connection that keeps your delete request from neither succeeding nor failing.
It is possible to avoid this behaviour by listening to onversionchange on your opened connections and make sure to close the connection when that event is triggered.

Okay as it turns out all of my code was good but deleting a database a second time takes somwhere between 10 seconds and several minutes.
you can test this for yourself: the code in this post shows it
what was happening is that jasmine times out after 5 seconds by default (i changed this to a higher number but who would think i takes THAT long)
browser: Chrome 39.0.2171.71 m

Make sure when deleting
the RESULT from DB creation is not empty
close the RESULT before deleting
delete through the same connection you created
var init=function(){
var request = this.connection.open(name,version);
request.onupgradeneeded=function(e){
var version=e.target.result;
};
request.onsuccess=function(e){
db.result=e.target.result;
};
request.onerror=function(e){
};
};
var remove=function(){
if (typeof db.result !== 'undefined') {
db.result.close();
return this.connection.deleteDatabase(version);
}
}

This is 5 years late, but just in case there are more guys like me...
close the connection to the database before deleting it...

Related

Protractor Random Alert Box "Error:jQuery not found."

jQuery Error
I've done some extensive searching, and have found samples of code to handle an alert box when you expect it; but I haven't been able to find anything on handling a random alert box that might, or might not appear.
The website I'm dealing with is very stubborn to begin with. Several elements without any kind of ID's, timeouts, network failures, etc.
98% of the time when I run the tests, they run without getting the alert box error and everything is good. When the alert box does popup the other 2% of the time which says "Error:jQuery not found," all my other tests fail with unexpected alert errors.
My first question is, could it be something in my code that's causing the error to happen? (see code below) My gut tells me it's probably the website. So if that's the case, could someone please show me an example that would handle a "possible" alert box and accept it, without failing my test? The swithTab() test is running first, and then the setDates() test is running next. The alert box error pops up after the switch tab, as the page is loading. I've tried using a deferred promise to handle the alert, and catch the error, but it fails before it can even catch the error. It fails as soon as it hits browser.switchTo().alert() because the alert usually doesn't exist. I really appreciate any help I could get.
this.switchTab = function(){
browser.getAllWindowHandles().then(function(handles){
browser.switchTo().window(handles[1]);
browser.sleep(2000);
var lockBoxTitle = element(by.css('td.title'));
browser.driver.wait(EC.visibilityOf(lockBoxTitle),5000);
});
}
this.setDates = function(yesterdayDate){
browser.sleep(3000);
//handleAlert();
startDateTextBox.clear();
startDateTextBox.sendKeys(yesterdayDate);
endDateTextBox.clear();
endDateTextBox.sendKeys(yesterdayDate);
retrieveBtn.click();
browser.sleep(5000);
expect(validateStart.getText()).toEqual(yesterdayDate);
expect(validateEnd.getText()).toEqual(yesterdayDate);
}
You can check if popup is displayed and if is displayed click the button for closing it:
var popUpElm = element(by.model(""));
var closeBtn= element(by.model(""));
popUpElm.isDisplayed().then(function(isDisplayed) {
if (isDisplayed) {
closeBtn.click();
console.log("Popup is closed");
} else {
console.log("Popup is not displayed");
}
});

Error handling "Out of Memory Exception" in browser?

I am debugging a javascript/html5 web app that uses a lot of memory. Occasionally I get an error message in the console window saying
"uncaught exception: out of memory".
Is there a way for me to gracefully handle this error inside the app?
Ultimately I need to re-write parts of this to prevent this from happening in the first place.
You should calclulate size of your localStorage,
window.localStorage is full
as a solution is to try to add something
var localStorageSpace = function(){
var allStrings = '';
for(var key in window.localStorage){
if(window.localStorage.hasOwnProperty(key)){
allStrings += window.localStorage[key];
}
}
return allStrings ? 3 + ((allStrings.length*16)/(8*1024)) + ' KB' : 'Empty (0 KB)';
};
var storageIsFull = function () {
var size = localStorageSpace(); // old size
// try to add data
var er;
try {
window.localStorage.setItem("test-size", "1");
} catch(er) {}
// check if data added
var isFull = (size === localStorageSpace());
window.localStorage.removeItem("test-size");
return isFull;
}
I also got the same error message recently when working on a project having lots of JS and sending Json, but the solution which I found was to update input type="submit" attribute to input type="button". I know there are limitations of using input type="button"..> and the solution looks weird, but if your application has ajax with JS,Json data, you can give it a try. Thanks.
Faced the same problem in Firefox then later I came to know I was trying to reload a HTML page even before setting up some data into local-storage inside if loop. So you need to take care of that one and also check somewhere ID is repeating or not.
But same thing was working great in Chrome. Maybe Chrome is more Intelligent.

Javascript debug no console error

My script dont run till the end. And stops due to errors. But there is no console error output.
alert("yo");
var go = "";
go.push(null);
alert("yo2");
first alert works
Tested with Chrome and Firefox
No try catch blocks
Are there reasons for not showing up a console error, without knowing the whole code?
You should see the error: TypeError: Object has no method 'push'
Im getting it in chrome console.
The error message is saying that the push method cannot be run on the variable go.
This is because it is of the type String and thus does have the push method available.
The push method is used to add items into an Array like this:
go = []; // or you could use `go = Array()`;
go.push("A string");
go.push(null);
To output messages to the browser console, use console.log like this:
console.log("Some debug message");
console.log(go); // outputs the above array
I solved the problem by using try-catch:
try
{
var my_app = new My_App();
}
catch(e)
{
console.log("ERROR"+e);
}
Now i get the console output.

Suppressing HTMLImageElement onerror [duplicate]

This question already has an answer here:
Check if file exists but prevent 404 error in console from showing up [duplicate]
(1 answer)
Closed 8 years ago.
I'm running a script that is dynamically loading images depending on a few criteria. The script does not know beforehand whether a specific image source actually exists and will thus need to check before displaying the image. I do this by replacing the onerror handler on the element with a function that attempts to gracefully handle the event.
At first glance this works rather well, however even though I have replaced the event, the browser still audits 404 errors in the console which I don't want. Even worse is that IE displays the infamous JS error icon in the status bar which I find rather awkward.
I've tried to summarise the problem in a JSFiddle.
var img = new Image();
img.onerror = function (ev) {
alert("This does not exist!");
return true;
}
img.onload = function (ev) {
document.body.appendChild(this);
}
img.src = "foo.png"; //Missing image
Basically, I want to suppress all error reporting for this element such that the console doesn't get flooded with superfluous error output.
I know that I could solve this by prefetching and evaluating the HTTP headers with AJAX and server side scripting, which while technically a possible solution, is something I would prefer to avoid. However, while I only use standard JS in my example, JQuery code is also acceptable.
I have tried reading up on the event specification, but since web scripting is still the mess of confusing ECMAScript, HTML DOM, client, pixy dust and now HTML5 definitions that we all love to hate, it really didn't make me any wiser. The closest I got was Mozilla's documentation (that interestingly doesn't even state the correct function parameters) which suggested that letting the event return true would suppress errors, but that didn't really work.
I believe you can not check if image link is broken/does not exist without getting 404 error. Which is actually is information about link is broken.
You mentioned that other way is ajax to check existance...
function UrlExists(url) {
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status != 404;
}
UrlExists('img_url');
but still you will get 404 in console.

Phonegap App Error - connection to the server was unsuccessful

I have the following issue and I'm a bit new to Phonegap! On my index page I have three functions that will create a Javascript Prompt asking the user for their name, email and title (position) and store each to the localStorage. Three items like this:
function promptName(){
var salesPName = prompt("Bitte geben Sie Ihren Namen","");
if(salesPName == null || salesPName == ""){
promptName()
}else{
localStorage.setItem("salesP", salesPName);
}
}
Then using $(document).ready I call these three functions:
$(document).ready(function(){
if(!localStorage.getItem("salesP")){
promptName();
promptEmail();
promptPosition();
}
});
This is all working well, however when deploying to my Android device I get the prompts but before I can complete all three I get an error dialog stating:
The connection to the server was unsuccessful (file:///android_asset/www/appname/index.html)
Removing the prompts removes the error but I need this functionality. I have tried different ways of calling the functions, for example on the body tag's onload event or using .load(). I still get this error. I thought about setting a Javascript interval to call this after a few seconds (once the page is loaded) as I'm sure the problem is due to Javascript's blocking nature. Has anyone come across this before?
Please note that I added the following to the com.mypackage.xxx.java file (as advised from phonegap, connection to server unsuccessful)
super.setIntegerProperty("loadUrlTimeoutValue", 10000);
And I still get the problem!
with nothing working I put a setTimeout() around my condition like so...
$(document).ready(function(){
setTimeout(function (){if(!localStorage.getItem("salesP")){
// item doesn't exist... so let's raise some dialogs to capture the name, email address and title
promptName();
promptEmail();
promptPosition();
}
}, 5000)
});
now it works fine... a bit of a fudge but so what, if anyone has any ideas on a better solution or any objections to this please let me know

Categories

Resources