WebStorage - executeSql给undefined不是for循环中的函数

时间:2014-09-25 16:40:15

标签: javascript web-storage

这是我的代码:

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

2 个答案:

答案 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)

  1. onDbError未定义
  2. tx不是tx2
  3. 插入executeSql而不是execuseSql
  4. 应该工作

    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);
    }