2015-12-30 66 views
3

請參閱下面的代碼。我叫setQuestion()內的db.transaction sucessCallBack但收到此錯誤Uncaught TypeError: this.setQuestions is not a function。我的代碼中有任何錯誤。請幫幫我。幫助將不勝感激。謝謝成功回調不調用的WebSQL交易的另一個功能

game.module(
"game.scenes.scene" 
) 
.require(
    "game.assets", 
    "game.entities.gameObject", 
    "game.entities.backgroundObject", 
    "game.entities.animeObject", 
    "game.entities.starObject", 
    "game.entities.buttonObject" 
).body(function() { 
    game.SceneScene1 = game.Scene.extend({ 
    loaded: function() { 
     this.get_difficulty_level(); 
    }, 
    setQuestions: function() { 
     //some code 
    }, 
    get_difficulty_level: function(){ 
     var app_id = this.app_id; 
     var user_id = this.user_id; 
     db.transaction(function (transaction) 
     { 
      transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [], 
      function (transaction, result) 
      { 
       if (result.rows.length == 0) 
       { 
        difficulty_level=2; 
       }else{ 
        difficulty_level = result.rows.item(0).difficulty; 
        console.log(result.rows.item(0)); 
       } 
      }); 
      },this.errorHandler,this.successDiffi); 
     }, 
     successDiffi: function(){ 
      this.setQuestions(); 
     }, 
}); 
}); 
+0

您是否在發佈時遺失了一些代碼? '''.body(function(){'打開一個匿名函數,但是下面的代碼定義了一個對象的屬性。 – puqeko

+0

是的,我錯過了,編輯我的問題 – Vinie

回答

0

發生這種情況是因爲成功回調在其他範圍內調用,而不是您所假定的。要解決這個問題,您可以嘗試在回調中使用關閉:

game.module(
"game.scenes.scene" 
) 
.require(
    "game.assets", 
    "game.entities.gameObject", 
    "game.entities.backgroundObject", 
    "game.entities.animeObject", 
    "game.entities.starObject", 
    "game.entities.buttonObject" 
).body(function() { 
    game.SceneScene1 = game.Scene.extend(
    new function() 
    { 
    this.loaded = function() { 
     this.get_difficulty_level(); 
    }; 
    this.setQuestions= function() { 
     //some code 
    }; 
    this.get_difficulty_level= function(){ 
     var app_id = this.app_id; 
     var user_id = this.user_id; 
     db.transaction(function (transaction) 
     { 
      transaction.executeSql('SELECT * FROM User_report where app_id="'+app_id+'" and user_id="'+user_id+'" order by id desc limit 1;', [], 
      function (transaction, result) 
      { 
       if (result.rows.length == 0) 
       { 
        difficulty_level=2; 
       }else{ 
        difficulty_level = result.rows.item(0).difficulty; 
        console.log(result.rows.item(0)); 
       } 
      }); 
      },this.errorHandler,this.successDiffi); 
     }; 
     var _this = this; 
     this.successDiffi= function(){ 
      _this.setQuestions(); 
     }; 
}); 
}); 
+0

謝謝你的想法。我剛剛使用了'var _this = this; this.successDiffi =函數(){ _this.setQuestions(); };'接近。 – Vinie