0

嗨,我使用下面的代码来读取文本文件并将其内容导入本地 SQLite。

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

    }
});
}

但我收到以下错误

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]

我哪里错了?

4

3 回答 3

1

您的第一个问题是,当它们在 JavaScript 中基于 0 时,您将数组用作 1。
这个

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

应该

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

另一个问题是您在循环中创建匿名函数,如果匿名函数在循环的下一次迭代之前没有执行,则值i将改变,您可以使用 IIFE 返回一个函数,该函数将使用iin循环的当前迭代。

            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));
于 2012-09-20T20:15:38.247 回答
0

你的 for 循环应该从 0 开始。

for (i = 0; i < (mybilljson.posts.length); i++) {
  ....
}
于 2012-09-20T19:17:24.620 回答
0
for (i = 1; i <= (mybilljson.posts.length); i += 1) {

在 javascript 中,数组从零开始索引。相反,请尝试:

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

坦率地说,你的代码有点乱。我很幸运我发现了这个错误。可能还有其他人。

于 2012-09-20T19:14:57.193 回答