2012-09-20 49 views
0

您好我使用下面的代碼來讀取文本文件並將其內容導入到本地SQLite中。當我嘗試將Json導入到SQLite時,爲什麼會收到Uncaught TypeError?

function billtxt() 
{ 
    var billjson = '{"posts" : [', 
     i, 
     line = 0, 
     bill = 0, 
     Amountint, 
     Amountdec; 

     jQuery.ajaxSetup({ 
       'beforeSend' : function(xhr) { 
        xhr.overrideMimeType('text/plain; charset=iso-8859-7'); 
       }, 
     }); 

     jQuery.get('Bill.txt',function(data){ 
      var mybill = []; 
      alert(data.length); 
      line=0; 
      for (i = 1; i <= ((data.length)/156); i += 1) { 
       billjson += '{"Id" :' + '"' + data.substr((line+0), 10).trim() + '"' + ','; 
       billjson += '"Code" :' + '"' + data.substr((line+10), 5).trim() + '"' + ','; 
       billjson += '"Address" :' + '"' + data.substr((line+14), 40).trim() + '"' + ','; 
       billjson += '"Name" : ' + '"' + data.substr((line+54), 50).trim() + '"' + ','; 
       billjson += '"Description" : ' + '"' + data.substr((line+104), 8).trim() + '"' + ','; 
       billjson += '"EntrySeason" : ' + '"' + data.substr((line+112), 23).trim() + '"' + ','; 
       billjson += '"Period" : ' + '"' + data.substr((line+112), 23).trim() + '"' + ','; 
       Amountint = data.substr((line+146), 7).trim(); 
       Amountdec = data.substr((line+153), 2).trim(); 
       billjson += '"Revenue" : ' + '"' + Amountint + '.' + Amountdec + '"' + '}'; 
       line = i * 156; 
       if (line == data.length) 
       { 
        billjson += ']'; 
       } 
       else 
       { 
        billjson += ','; 
       } 
      } 

      if (line == 0) 
      { 
       billjson += ']'; 
      } 


      billjson += "}"; 

      alert(billjson); 
      var mybilljson = jQuery.parseJSON(billjson); 
      alert(mybilljson.posts.length); 
     for (i = 0; i < (mybilljson.posts.length); i += 1) { 

      $.mobile.notesdb.transaction((function(i) { 
       return function(t){ 
console.log(mybilljson.posts[i].Name); 
        t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount) VALUES (?,?,?,?,?,?,?,?);', 
         [mybilljson.posts[i].Id, mybilljson.posts[i].Code, mybilljson.posts[i].Address, mybilljson.posts[i].Name, mybilljson.posts[i].Description, mybilljson.posts[i].EntrySeason, mybilljson.posts[i].Period, mybilljson.posts[i].Revenue] 
          ); 
       } 
      })(i)); 

    } 
}); 
} 

但我reaceive下面的錯誤

Uncaught TypeError: Cannot read property 'Id' of undefined 

在低於行

[mybilljson.posts[i].Id, mybilljson.posts[i].Code, mybilljson.posts[i].Address, mybilljson.posts[i].Name, mybilljson.posts[i].Description, mybilljson.posts[i].EntrySeason, mybilljson.posts[i].Period, mybilljson.posts[i].Revenue] 

我在哪裏錯了?

+0

此外,請向我們顯示所有警報語句的輸出。 – FrankieTheKneeMan

+0

是的,你仍然需要一個循環來填充函數參數'i' – FrankieTheKneeMan

回答

1

你的第一個問題是,您使用的陣列爲1爲主,當它們是基於在JavaScript 0。

for (i = 1; i <= (mybilljson.posts.length); i += 1) { 

應該

for (i = 0; i < (mybilljson.posts.length); i += 1) { 

的另一個問題是,你是在一個循環中創建匿名函數,如果匿名函數沒有環路的值的下一次迭代前執行i會發生變化,您可以使用IIFE返回一個函數,該函數將在循環的當前迭代中使用值i

  notesdb.transaction((function(i){ 
       return function(t) { 
        t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount) VALUES (?,?,?,?,?,?,?,?);', 
         [mybilljson.posts[i].Id, mybilljson.posts[i].Code, mybilljson.posts[i].Address, mybilljson.posts[i].Name, mybilljson.posts[i].Description, mybilljson.posts[i].EntrySeason, mybilljson.posts[i].Period, mybilljson.posts[i].Revenue] 
          ); 
       } 
      })(i)); 
+0

這是第二個問題。我甚至沒有看到它。 – FrankieTheKneeMan

+0

代碼更新與您的解決方案...我沒有收到任何錯誤,但都沒有填寫帳單。 – kosbou

+0

代碼已更新。我理解對嗎?因爲帳單仍然是空的... – kosbou

0
for (i = 1; i <= (mybilljson.posts.length); i += 1) { 

在javascript中,數組從零開始索引。相反,請嘗試:

for (i = 0; i < (mybilljson.posts.length); i++) { 

坦率地說,你的代碼有點亂。我很幸運,我發現了這個錯誤。可能還有其他人。

+0

同樣的錯誤未捕獲TypeError:無法讀取屬性'Id'undefined – kosbou

+0

在問題中發佈您的更新代碼。 – FrankieTheKneeMan

+0

代碼已更新。上面的代碼越多,讀取一個固定大小的數據的文本文件,並將其轉換爲JSON ...然後,我創建一個循環,以便將JSON導入到SQLite白衣收到上述錯誤。 – kosbou

0

您的循環應該開始從0

for (i = 0; i < (mybilljson.posts.length); i++) { 
    .... 
} 
+0

同樣的錯誤未捕獲TypeError:無法讀取未定義的屬性'Id' – kosbou

相關問題