0

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

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.