Skip to content
Advertisement

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'
}

Advertisement

Answer

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.

enter image description here

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

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement