2015-11-04 58 views
0

我在多個網站上部署了應用程序。所有UI錯誤都發送到我們的服務器進行日誌記錄。 我們得到以下錯誤。設置Cookie時出錯 - 無法讀取null的屬性'1'

上下文:使用來自w3school(link)的代碼,使用普通javascript(無jQuery)設置cookie。

瀏覽器:Chrome。

版本:45.0.2454.101

操作系統:Linux

TypeError: Cannot read property '1' of null  
at _parseKeyValuePair (eval at <anonymous> (unknown source), <anonymous>:123:22) 
at parseCookie (eval at <anonymous> (unknown source), <anonymous>:73:16) 
at HTMLDocument.Object.defineProperty.set [as cookie] (eval at <anonymous> (unknown source), <anonymous>:176:18) 
at setCookie (script.min.js:1:3446) 
at Array.widgetLoaded (script.min.js:1:11687) 
at handleMessage (script.min.js:1:1640) 
at eval (eval at <anonymous> (unknown source), <anonymous>:401:13) 

我無法找到谷歌的任何類似的例外。任何人?

回答

0

我覺得_parseKeyValuePair調用正則表達式,而不檢查它像下面的代碼有效訪問它的結果:

function _parseKeyValuePair(str) { 
    var result = str.match(/(\w+)=(.+?);/); 
    return {key: result[1], value: result[2]); 
} 

result可以null如果正則表達式不匹配的字符串。在訪問它之前應檢查result是否爲null,如果不是null,則使用它。

例如,上面的函數可以寫成這樣:

function _parseKeyValuePair(str) { 
    var result = str.match(/(\w+)=(.+?);/); 

    if (result !== null) { 
    return {key: result[1], value: result[2]); 
    } 
} 

雖然它不是一個確切的解決方案,因爲我不知道真正的_parseKeyValuePairparseCookie做,我敢肯定,這可以幫助你。

+0

我認爲這有幫助:)所以,你所說的是,爲了reprocude它,我需要訪問非現有的cookie條目。那是對的嗎? – yonatan

+0

它可以。你能告訴我你的'_parseKeyValuePair()'嗎?給你一個更正確的答案是有幫助的。 – taggon

+0

這是一個瀏覽器內部功能,不是我的... – yonatan