gpt4 book ai didi

javascript - WebStorage -executeSql 给出未定义不是 for 循环中的函数

转载 作者:行者123 更新时间:2023-12-02 16:57:45 25 4
gpt4 key购买 nike

这是我的代码:

给定一个对象,我尝试在 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);
}
有什么想法吗?

最佳答案

  1. onDbError 未定义
  2. tx 不是 tx2
  3. 插入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);
}

关于javascript - WebStorage -executeSql 给出未定义不是 for 循环中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com