0

フォーラムで質問せざるを得ないのは本当に申し訳ありませんが、私はこの問題を解決するために一日中頭を悩ませてきました.

私は、SQLite データベースを作成し、テーブルを作成し、tinternet から JSON フィードを取得し、そのデータをテーブルにフィードする phonegap アプリを持っています。

データベースにデータが入力されるはずの部分までは、すべて問題ないようです。「insertProperty」関数は実行されません。どんな助けでも本当に素晴らしいでしょう!

関連するコード スニペットを次に示します。

x$(document).on('deviceready', function () {

    // CREATE DATABASE
var localDatabase = window.sqlitePlugin.openDatabase("localDB", "1.0", "Local DB", 5000000);
console.log("Database Loaded");
// CREATE properties TABLE
var query = "CREATE TABLE IF NOT EXISTS properties (propertyid INT, propertytype text, address text,  postcode text, clientid INT);"
db.transaction(function (trxn) {
    trxn.executeSql(query,  [],  callback,
    function (transaction, error) { //error callback
        console.log(error);
    });
});
}



function insertProperty(db, data) {
var query = "INSERT INTO properties (propertyid, propertytype, address, postcode, clientid) VALUES (?,?,?,?,?);"
db.transaction(function (trxn) {
    trxn.executeSql(query,[data.propertyid, data.propertytype, data.address, data.postcode, data.clientid],
    function (transaction, resultSet) {
        console.log('success');
    },
    function (transaction, error) {
        console.log(error);
    }
    );
});
}




x$().xhr("http://myurl.com", { // GET JSON FEED

    async: true,callback: function () { try {
        var dataArray = JSON.parse(this.responseText); // PARSE JSON FEED
        console.log("Loading live data");
        dataArray.forEach(function (thedata) { // LOOP DATA
            console.log(thedata.address);
            insertProperty(localDatabase, thedata);
        });
         console.log("Rendering Live Data");
        renderDataset(dataArray)
    } catch (e) { // failed to retrieve data
            console.log("There was a problem, Loading local data");
            getStoredProperties(localDatabase, function (offlineData) {
                renderDataset(offlineData);
            });
         }
    }
});

スクリプトは「console.log(thedata.address);」まで実行されます。アドレスが表示されますが、insertProperty 関数を実行しようとするとすぐに、「console.log("There was a problem, Loading local data");」に直行します。少し。

ご協力いただきありがとうございます。ダン

4

1 に答える 1

0

どうやら「deviceready」関数のスコープでローカル変数localDatabaseを定義しているため、xhr コールバック関数内でデータをループしている場合はアクセスできません。

于 2012-10-24T20:34:58.777 に答える