简体   繁体   中英

JavaScript Array objects not printing to console

I have a code snippet that is suppose to print array object to log but not working wondering if someone could help. I am trying to get the subscriptionExpirationDate and item

What works

const onSessionConnect = (event) => {
    if (event.purchaseState == 'PURCHASED') {
       console.log('plan list is found here ', event.PurchaseState);
       
    }

Console prints Purchased

What Does not work

const onSessionConnect = (event) => {
    if (event.purchaseState == 'PURCHASED') {
       console.log('plan list is found here ', event.purchases.skuIdentifier);
       
    }

console prints Undefined

Could someone explain how to get the other objects to print in the console

Here is the array

{
  purchases: [{
    skuIdentifier: '199_1m_1w0',
    subscriptionExpirationDate: '2020-11-15T06:12:57Z',
    purchaseSource: 'USER',
    transactionIdentifier: 'BPS-74511616-4E51-42F7-A528-DE15A8FF0279'
  }],
  purchaseState: 'PURCHASED'
}

purchases is an array and you are trying to access it like the object. You can access it like

console.log('plan list is found here ', event.purchases[0].skuIdentifier);

purchases is an array of JSON objects and hence you need to access 0th index of purchases.

console.log('plan list is found here ', event.purchases[0].skuIdentifier);

Whenever you are stuck in these kind of issues, print out the main object in console.log and then you will see the complete definitition of the object in Developer console.

As you can see here, purchases has 1 in the brackets followed by square brackets which signifies that x.purchases is an array with 1 element in it.

在此处输入图像描述

And if you expand this, Developer console will show it in a nice way as follows

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM