使用 Web 数据库时出现 Phonegap 错误:“表达式 mydb.transaction 的结果不是函数”
我第一次尝试在 iOS 应用程序中使用 html5 的网络数据库,并使用phonegap。但我陷入了这个错误,它说“表达式 mybd.transaction 的结果不是函数”
如果我使用警报进行检查,initDB 正在执行,但是当涉及到 createTables 函数时,会出现上述错误,我对此无能为力其上。
我已经使用了这个实现 -> http://wiki.phonegap.com/w/page/16494756/Adding%20SQL%20Database%20support%20to%20your%20iPhone%20App
<script type="text/javascript">
function validateFloat()
{
var mydb=false;
var fuelUnits = document.myForm.UnitsOfFuel;
var bFuelUnits = false;
var bUnitPrice = false;
switch (isNumeric(fuelUnits.value))
{
case true:
bFuelUnits = true;
fuelUnits.style.background="white";
break;
case false:
fuelUnits.focus();
fuelUnits.style.background="yellow";
break;
}
var unitPrice = document.myForm.PricePerUnit;
switch (isNumeric(unitPrice.value))
{
case true:
bUnitPrice = true;
unitPrice.style.background="white";
break;
case false:
unitPrice.focus();
unitPrice.style.background="yellow";
break;
}
if(bFuelUnits && bUnitPrice)
{
if(initDB(mydb))
{
if(createTables(mydb))
{
loadCelebs();
return true;
}
else
return false;
}
else
return false;
}
else
{
return false;
}
}
function isNumeric(n)
{
var n2 = n;
n = parseFloat(n);
return (n!='NaN' && n2==n);
}
// initialise the database
function initDB(mydb)
{
try
{
if (!window.openDatabase)
{
alert('not supported');
return false;
}
else
{
var shortName = 'phonegap';
var version = '1.0';
var displayName = 'PhoneGap Test Database';
var maxSize = 65536; // in bytes
mydb = openDatabase(shortName, version, displayName, maxSize);
alert("initDB");
return true;
}
}
catch(e)
{
// Error handling code goes here.
if (e == INVALID_STATE_ERR)
{
// Version number mismatch.
alert("Invalid database version.");
return false;
}
else
{
alert("Unknown error "+e+".");
return false;
}
return true;
}
}
// db error handler - prevents the rest of the transaction going ahead on failure
function errorHandler(transaction, error)
{
alert("errorHandler");
// returns true to rollback the transaction
return true;
}
// null db data handler
function nullDataHandler(transaction, results)
{
}
// create tables for the database
function createTables(mydb)
{
try
{
mydb.transaction(
function(tx) {
tx.executeSql('CREATE TABLE celebs(id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL DEFAULT "");', [], nullDataHandler(tx,results), errorHandler(tx,error));
tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx,results), errorHandler(tx,error));
tx.executeSql('insert into celebs (id,name) VALUES (2,"Keira Knightley");', [], nullDataHandler(tx,results), errorHandler(tx,error));
});
alert("createTables");
return true;
}
catch(e)
{
alert(e.message);
return false;
}
}
// load the currently selected icons
function loadCelebs()
{
try
{
mydb.transaction(
function(tx) {
tx.executeSql('SELECT * FROM celebs ORDER BY name',[], celebsDataHandler(tx,results), errorHandler(tx,error));
});
}
catch(e)
{
alert(e.message);
}
}
// callback function to retrieve the data from the prefs table
function celebsDataHandler(transaction, results)
{
alert("here also?");
// Handle the results
var html = "<ul>";
for (var i=0; i<results.rows.length; i++)
{
var row = results.rows.item(i);
html += "<li>"+row['name']+"</li>\n";
}
html +="</ul>";
alert(html);
}
</script>
I'm trying to use html5's web-database using phonegap for an iOS app for the first time. But I'm stuck at this error which says "result of expression mybd.transaction is not a function"
If I check using alerts, initDB is getting executed but when it comes to createTables function, the above error rises and I'm helpless from thereon.
I've used this implementation -> http://wiki.phonegap.com/w/page/16494756/Adding%20SQL%20Database%20support%20to%20your%20iPhone%20App
<script type="text/javascript">
function validateFloat()
{
var mydb=false;
var fuelUnits = document.myForm.UnitsOfFuel;
var bFuelUnits = false;
var bUnitPrice = false;
switch (isNumeric(fuelUnits.value))
{
case true:
bFuelUnits = true;
fuelUnits.style.background="white";
break;
case false:
fuelUnits.focus();
fuelUnits.style.background="yellow";
break;
}
var unitPrice = document.myForm.PricePerUnit;
switch (isNumeric(unitPrice.value))
{
case true:
bUnitPrice = true;
unitPrice.style.background="white";
break;
case false:
unitPrice.focus();
unitPrice.style.background="yellow";
break;
}
if(bFuelUnits && bUnitPrice)
{
if(initDB(mydb))
{
if(createTables(mydb))
{
loadCelebs();
return true;
}
else
return false;
}
else
return false;
}
else
{
return false;
}
}
function isNumeric(n)
{
var n2 = n;
n = parseFloat(n);
return (n!='NaN' && n2==n);
}
// initialise the database
function initDB(mydb)
{
try
{
if (!window.openDatabase)
{
alert('not supported');
return false;
}
else
{
var shortName = 'phonegap';
var version = '1.0';
var displayName = 'PhoneGap Test Database';
var maxSize = 65536; // in bytes
mydb = openDatabase(shortName, version, displayName, maxSize);
alert("initDB");
return true;
}
}
catch(e)
{
// Error handling code goes here.
if (e == INVALID_STATE_ERR)
{
// Version number mismatch.
alert("Invalid database version.");
return false;
}
else
{
alert("Unknown error "+e+".");
return false;
}
return true;
}
}
// db error handler - prevents the rest of the transaction going ahead on failure
function errorHandler(transaction, error)
{
alert("errorHandler");
// returns true to rollback the transaction
return true;
}
// null db data handler
function nullDataHandler(transaction, results)
{
}
// create tables for the database
function createTables(mydb)
{
try
{
mydb.transaction(
function(tx) {
tx.executeSql('CREATE TABLE celebs(id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL DEFAULT "");', [], nullDataHandler(tx,results), errorHandler(tx,error));
tx.executeSql('insert into celebs (id,name) VALUES (1,"Kylie Minogue");', [], nullDataHandler(tx,results), errorHandler(tx,error));
tx.executeSql('insert into celebs (id,name) VALUES (2,"Keira Knightley");', [], nullDataHandler(tx,results), errorHandler(tx,error));
});
alert("createTables");
return true;
}
catch(e)
{
alert(e.message);
return false;
}
}
// load the currently selected icons
function loadCelebs()
{
try
{
mydb.transaction(
function(tx) {
tx.executeSql('SELECT * FROM celebs ORDER BY name',[], celebsDataHandler(tx,results), errorHandler(tx,error));
});
}
catch(e)
{
alert(e.message);
}
}
// callback function to retrieve the data from the prefs table
function celebsDataHandler(transaction, results)
{
alert("here also?");
// Handle the results
var html = "<ul>";
for (var i=0; i<results.rows.length; i++)
{
var row = results.rows.item(i);
html += "<li>"+row['name']+"</li>\n";
}
html +="</ul>";
alert(html);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要返回在
initDB()
函数中创建的新创建的mydb
实例,然后使用返回的实例。如果您要为传递给函数的参数重新分配新值(这就是您正在做的事情),则需要返回该值,否则更改将丢失。
请注意,如果您将对象传递给函数(您没有这样做),则可以修改该对象的属性,并且这些更改将保留在该函数的范围之外。
vs...
当然,你传入的也是一个原始布尔值,而不是一个对象。基元按值传递,因此您必须返回新创建的
mydb
。更新
您还错误地使用了传入的事务处理程序。再次查看 Phone Gap Wiki,了解他们如何将函数引用分配给变量并将这些引用传递到事务方法中。就像现在一样,您正在调用函数而不是传递它们。
所以,不要这样做(你现在正在做的事情):
这样做:
我希望这能解决问题。另外,请记住,如果这回答了您的问题,请投票并将其标记为答案,以供未来访问者参考。
You need to return the newly created
mydb
instance that is created within theinitDB()
function and then use the returned instance.If you are reassigning a new value to a parameter that is passed into a function (which is what you are doing), it needs to be returned or the changes will be lost.
Note that if you are passing in an object to the function (which you are not doing), you can modify properties of that object and those changes will be persisted outside the scope of that function.
vs...
Of course, you are also passing in a primitive boolean value, not an object. Primitives are passed by value so you must return the newly created
mydb
.UPDATE
You are also using your passed-in transaction handlers wrong. Look at the phone gap wiki again to see how they are assigning the function references to variables and passing those references into the transaction methods. As it is now, you are calling the functions instead of passing them.
So, instead of this (what you are doing now):
do this:
I hope this clears it up. Also, remember, if this answered your question, upvote it and mark it as the answer for a reference to future visitors.