2013-01-08 86 views
6

以下は私のコードのスニペットです。未定義またはnull参照のプロパティを取得できません - Windows 8 JS/CSSアプリケーション

JavaScriptのランタイムエラー:私は取得していますエラーは、しかし、その後、次のエラーが返され、私が検索を実行し、メソッド_searchDataを呼び出すときに、それが成功した方法_lookUpSuccessを呼び出すことであるプロパティを取得できません「_displayResult」未定義の又はnull参照が_displayResultメソッドを呼び出そうと

なぜこれがかもしれ?で

(function() { 

    // make this an object property/method eventually 
    var displayResult = function (queryResult) { 
     for (var i = 0; i < holder.length; i++) { 
      //document.querySelector(".item-content .title").textContent = "FilmApp"; 
      document.querySelector(holder[i]).textContent = queryResult[i]; 
     }}; 

    // Creates a new page control with members 
    ui.Pages.define(searchPageURI, { 
     //... 
     _searchData: function (queryText) { 
      searchBase   = 'http://www.example.com/web-service2.php?termID='; 
      searchFormat  = 'JSON'; 
      searchFormatPiece = '&format=' + searchFormat; 

      if (!window.Data) { 
       var searchUrl = searchBase + queryText + searchFormatPiece; 
       WinJS.xhr({ url: searchUrl }).done(this._lookUpSuccess, this._lookUpFail, this._lookUpProgress); 
      }else{ 
       document.querySelector(".titlearea .pagetitle").textContent = "There has been a computer malfunction - sort it the **** out!"; 
      } 
     }, 

     _lookUpSuccess: function xhrSucceed(Result) { 
      var response = JSON.parse(Result.responseText); 
       if (response.response[0].Message === "Found") { 
        for (var x in response.response[1].term) { 
         content.push(response.response[1].term[x]); 
        }; 
        this._displayResult(content); 
        //displayResult(content); 
        return content; 
       } else { 
        content.push("Not Found", "Not Found"); 
        return content; 
       } 
     }, 

     _lookUpFail: function xhrFail(Result) { document.querySelector(".titlearea .pagetitle").textContent = "Got Error"; }, 
     _lookUpProgress: function xhrProgress(Result) { document.querySelector(".titlearea .pagetitle").textContent = "No Result 2"; }, 

     _displayResult: function (queryResult) { 
      var holder; 
      holder = [DefDiv, DescDiv]; 

      for (var i = 0; i < holder.length; i++) { 
       //document.querySelector(".item-content .title").textContent = "FilmApp"; 
       document.querySelector(holder[i]).textContent = queryResult[i]; 
      }; 
     }, 

    }); 

    // End of UI.page.define  

    // #2 This method is run on application load 
    WinJS.Application.addEventListener("activated", function (args) { 
     if (args.detail.kind === appModel.Activation.ActivationKind.search) { 
      args.setPromise(ui.processAll().then(function() { 
       if (!nav.location) { 
        nav.history.current = { location: Application.navigator.home, initialState: {} }; 
       } 
      return nav.navigate(searchPageURI, { queryText: args.detail.queryText }); 
      })); 
     } 
    }); 

    // #3 
    appModel.Search.SearchPane.getForCurrentView().onquerysubmitted = function (args) { nav.navigate(searchPageURI, args); }; 

})(); 

答えて

8

です:

WinJS.xhr({ url: searchUrl }).done(this._lookUpSuccess, this._lookUpFail, this._lookUpProgress); 

あなたが行わハンドラ関数として_lookupSuccessを渡していますしかし、それが呼び出されると、 thisの値は、それが完了したハンドラを呼び出す内部のものによって設定されるため、もはや望むものではありません。関数として関数this._lookUpSuccessを渡すだけで関数が渡されます。値はthisになりません。 this._lookUpSuccessが間違っthisで呼び出されたときに、その中のthisへの参照は、その上に期待される特性(あなたが見るので、エラーを)見つけることができません。

あなたは_lookUpSuccess()を呼び出すときに使用し、ローカル変数にthis値を保存し、それはこのようにそれを修正することができます:

var self = this; 
WinJS.xhr({ url: searchUrl }).done(function(result) {self._lookUpSuccess(result)}, this._lookUpFail, this._lookUpProgress); 

これはコールバックで適切なthis値を再び取り付けるための非常に一般的な回避策です。

+0

これはうまくいきましたが、 'lookUpSuccess'メソッドに新しい問題が導入されました。今、私が手に 'その改正案を作るかのように、未定義またはnull reference'のプロパティ「responseText」を取得できませんが、任意の値が_lookUpSuccess' – James

+0

'に渡されて停止している@James - はい、あなたは結果値に沿って渡す必要があります。私はあなたのやり方を示すために自分の答えを修正しました。注: 'this' ptrが必要な場合は、' _lookUpFail'と '_lookUpProgress'とまったく同じ問題があります。 – jfriend00

+0

これは完璧に働きました。このおかげで非常に感謝しています。知っているトラブルシューティングの非常に便利なビット! – James

2

:あり

this._displayResult(content); 

thisは、このコード行では明らかにundefinedまたはnull

+0

私はもともとこれを考えたが、その後、それは 'べきである提案し、以下のリンクを見たundefined' - http://stackoverflow.com/questions/9822561/why-is-this-in-an-anonymous-function-undefined- when-using-strict – James

関連する問題