0

在我的buy.js中,我首先檢查用戶以前是否購買過我的包。如果不是,我打電話給購買方法以啓動購買。應用內購買陣列Chrome擴展程序出錯

我目前得到的錯誤:

Error in event handler for (unknown): TypeError: Cannot read property 'details' of undefined 
    at Object.onLicenseUpdate [as success] (buy.js:52:45) 
    at buy.js:1:397 

截圖數組在控制檯和錯誤接受:

**Screenshot of the array received in the console and the error.**

以下是有問題的代碼:

function onLicenseUpdate(response) { 
     console.log("onLicenseUpdate", response); 
     var licenses = response.response.details; 
     var count = licenses.length; 
     for (var i = 0; i < count; i++) { 
      var license = licenses[i]; 
      purchasedPackages = licenses[i].response.details.sku; 
      if (purchasedPackages == skuInit) { 
      purchased = true; 
      } 
      addLicenseDataToProduct(license); 

     } 
     if (purchased == false) { 
      buyProduct(skuInit); 
     } 
     //console.log(""); 
     } 

完整Buy.js代碼:

/*...google chrome's code...*/ 

/*CODE FOR IN-APP PURCHASES*/ 


var prodButPrefix = "btnProdID-"; 
var statusDiv; 

function init() { 
    console.log("App Init"); 
    //statusDiv = $("#status"); 
    // getProductList(); 

} 

/******************************* 
INITIATE PURCHASE 
******************************/ 
$("#xmas").click(function() { 
    checkIfPurchased("xmas"); 
    localStorage.purchasedXmas2016 = true; 
}); 
/*******************************/ 



/******************************** 
Get purchased items 
********************************/ 
var purchasedPackages; 
var purchased = false; 
function checkIfPurchased(sku){ 
    var skuInit = sku; 
    getLicenses(); 
function getLicenses() { 
    console.log("google.payments.inapp.getPurchases"); 
    console.log("Retreiving list of purchased products..."); 
    google.payments.inapp.getPurchases({ 
    'parameters': {env: "prod"}, 
    'success': onLicenseUpdate, 
    'failure': onLicenseUpdateFailed 
    }); 
} 

function onLicenseUpdate(response) { 
    console.log("onLicenseUpdate", response); 
    var licenses = response.response.details; 
    var count = licenses.length; 
    for (var i = 0; i < count; i++) { 
    var license = licenses[i]; 
    purchasedPackages = licenses[i].response.details.sku; 
    if(purchasedPackages == skuInit){ 
     purchased = true; 
    } 
    addLicenseDataToProduct(license); 

    } 
    if (purchased == false){ 
    buyProduct(skuInit); 
    } 
    //console.log(""); 
} 

function onLicenseUpdateFailed(response) { 
    console.log("onLicenseUpdateFailed", response); 
    console.log("Error retreiving list of purchased products."); 
} 
} 



function getLicenses() { 
    console.log("google.payments.inapp.getPurchases"); 
    console.log("Retreiving list of purchased products..."); 
    google.payments.inapp.getPurchases({ 
    'parameters': {env: "prod"}, 
    'success': onLicenseUpdate, 
    'failure': onLicenseUpdateFailed 
    }); 
} 

function onLicenseUpdate(response) { 
    console.log("onLicenseUpdate", response); 
    var licenses = response.response.details; 
    var count = licenses.length; 
    for (var i = 0; i < count; i++) { 
    var license = licenses[i]; 
    addLicenseDataToProduct(license); 
    } 
    //console.log(""); 
} 

function onLicenseUpdateFailed(response) { 
    console.log("onLicenseUpdateFailed", response); 
    console.log("Error retreiving list of purchased products."); 
} 



/****************** 
* Purchase an item 
****************/ 


function buyProduct(sku) { 
    console.log("google.payments.inapp.buy", sku); 
    //var sku = ""; 
    google.payments.inapp.buy({ 
    'parameters': {'env': 'prod'}, 
    'sku': sku, 
    'success': onPurchase, 
    'failure': onPurchaseFailed 
    }); 
} 


function onPurchase(purchase) { 
    console.log("onPurchase", purchase); 
    var jwt = purchase.jwt; 
    var cartId = purchase.request.cardId; 
    var orderId = purchase.response.orderId; 
    console.log("Purchase completed. Order ID: " + orderId); 
    getLicenses(); 
} 

function onPurchaseFailed(purchase) { 
    console.log("onPurchaseFailed", purchase); 
    var reason = purchase.response.errorType; 
    console.log("Purchase failed. " + reason); 
} 


/******************************* 
Update UI Buttons 
********************************/ 
if(localStorage.purchasedXmas2016 == true){ 
    var text = document.getElementById("xmas").firstChild; 
    text.data = "purchased"; 
    document.getElementById("xmas").style = "border-style:none"; 
} 

manifest.json的

{ 
    "manifest_version": 2, 

    "name": "", 
    "options_page": "options.html", 

    "background": { 
     "scripts": [ 
      "javascripts/background.js" 
     ] 
    }, 
    "description": "", 
    "version": "4.0.4.1", 
    "offline_enabled": true, 
    "browser_action": { 
    "default_icon": "icons/icon.png", 
    "default_popup": "popups/popup1.html" 
    }, 
    "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'", 

    "icons": { "16": "icons/icon16.png", 
       "32": "icons/icon32.png", 
      "48": "icons/icon48.png", 
      "64": "icons/icon64.png", 
      "128": "icons/icon128.png" }, 

    "permissions": [ 
    "activeTab", 
    "https://ajax.googleapis.com/", 
    "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js", 
    "storage", 
    "tabs" 
    ] 

} 

我也知道這是不是最有效的方式。有沒有辦法將所有購買的SKU存儲在一個數組中(或其他有效的方式)?

+0

爲什麼你重新定義函數'checkIfPurchased()'範圍內的相同或幾乎相同的代碼使用'getLicenses()','onLicenseUpdate()'和'onLicenseUpdateFailed()'? – Makyen

+0

請編輯問題爲主題:包括一個**完整** [mcve]複製問題。通常包括一個* manifest.json *,一些背景*和*內容腳本。尋求調試幫助的問題(「**爲什麼不是這個代碼工作?」)必須包括:►期望的行爲,►特定問題或錯誤*和*►在問題中重現問題所需的最短代碼**本身**。沒有明確問題陳述的問題對其他讀者無益。請參閱:「**如何創建[mcve] **」,[我可以在此處詢問哪些主題?](http://stackoverflow.com/help/on-topic)和[問]。 – Makyen

+0

@Makyen我已經添加了我的manifest.json,並指出問題似乎存在的地方(代碼片段)。你能否檢查一下,並提出一些方法來有效解決這個問題?謝謝! –

回答

0

details數組,沒有response屬性。該數組已經是包含sku屬性的對象列表。

爲了解碼response,你會需要你的onLicenseUpdate()功能更像:

function onLicenseUpdate(response) { 
    console.log("onLicenseUpdate", response); 
    var purchased = false; 
    var licenses = response.response.details; 
    var count = licenses.length; 
    for (var i = 0; i < count; i++) { 
    var license = licenses[i]; 
    purchasedPackages = license.sku; 
    if(purchasedPackages == skuInit){ 
     purchased = true; 
    } 
    addLicenseDataToProduct(license); 
    } 
    if (purchased == false){ 
    buyProduct(skuInit); 
    } 
    //console.log(""); 
} 

雖然,我會用forEach代替for

function onLicenseUpdate(response) { 
    console.log("onLicenseUpdate", response); 
    var purchased = false; 
    var licenses = response.response.details; 
    licenses.forEach(license=>{ 
    purchasedPackages = license.sku; 
    if(purchasedPackages == skuInit){ 
     purchased = true; 
    } 
    addLicenseDataToProduct(license); 
    } 
    if (purchased == false){ 
    buyProduct(skuInit); 
    } 
    //console.log(""); 
} 
+0

謝謝!我現在得到這個錯誤: '(未知)的事件處理程序中的錯誤:ReferenceError:addLicenseDataToProduct未定義 at Object.onLicenseUpdate [as success](buy.js:57:5) at buy.js:1 :397' –

+0

@ I-Parliament,我很高興能夠提供幫助。該函數addLicenseDataToProduct()並未在您提供的代碼中定義。鑑於您提供的代碼不是完整的擴展名,因此無法提供幫助。如果您想解決該問題,請創建另一個問題,但包含更多代碼。如果你確實創建了另一個問題,那麼包含這個問題的鏈接以提供上下文可能是一個好主意。 – Makyen