这是我的代码:
给定一个对象,我试图在webStorage数据库中插入一些数据。问题是控制台给我一个“未定义的不是函数”,我不知道为什么
以下是我的代码片段
try {
db = openDatabase("mca", "1.0", "db mca", 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS planning_x_material(id INTEGER PRIMARY KEY AUTOINCREMENT, planning_id INTEGER, material_id INTEGER, material_name TEXT)", []);
}, onDbError, fillDb);
} catch(e) {
alert("webstorage error");
}
function callback_planning_material(obj) {
console.log(obj);
db.transaction(function(tx) {
for(var k = 0; k < obj.length; k++) {
for (var i = 0; i < obj[k].material.length; i++) {
(function (k, i) {
var planning = obj[k];
var material = planning.material[i];
tx.execuseSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]); // here console gives me "undefined is not a function" error
})(k, i);
}
}
}, onDbError);
}
function fillDb() {
// example object
var obj = [
{
id: 1,
name: "name 1",
material: [
{ id: 1, name: "mat. 1" },
{ id: 2, name: "mat. 2" },
]
},
{
id: 2,
name: "name 2",
material: [
{ id: 3, name: "mat. 3" },
{ id: 4, name: "mat. 4" },
]
}
];
callback_planning_material(obj);
}
答案 0 :(得分:1)
tx2
未定义,不应该是tx
?
tx.execuseSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]);
答案 1 :(得分:1)
onDbError
未定义tx
不是tx2
executeSql
而不是execuseSql
应该工作
try {
db = openDatabase("mca", "1.0", "db mca", 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS planning_x_material(id INTEGER PRIMARY KEY AUTOINCREMENT, planning_id INTEGER, material_id INTEGER, material_name TEXT)", []);
}, onDbError, fillDb);
} catch(e) {
console.log("webstorage error",e);
}
function onDbError(tx, err){
console.log('There is error', err);
}
function callback_planning_material(obj) {
console.log(obj);
db.transaction(function(tx) {
for(var k = 0; k < obj.length; k++) {
for (var i = 0; i < obj[k].material.length; i++) {
(function (k, i) {
var planning = obj[k];
var material = planning.material[i];
tx.executeSql("INSERT INTO planning_x_material(planning_id, material_id, material_name) VALUES (?, ?, ?)", [planning.id, material.id, material.name]); // here console gives me "undefined is not a function" error
})(k, i);
}
}
}, onDbError);
}
function fillDb() {
// example object
var obj = [
{
id: 1,
name: "name 1",
material: [
{ id: 1, name: "mat. 1" },
{ id: 2, name: "mat. 2" },
]
},
{
id: 2,
name: "name 2",
material: [
{ id: 3, name: "mat. 3" },
{ id: 4, name: "mat. 4" },
]
}
];
callback_planning_material(obj);
}