簡體   English   中英

React-Native object 的子級返回 undefined 盡管它存在

[英]React-Native Child of object returns undefined despite it exists

我有一個非常基本但令人震驚的問題,我無法弄清楚。 這是這樣的:

問題的簡短摘要

我收到了來自 IAP 的receipt object,但無法獲得productId孩子。 它返回undefined 怎么會發生? 收據.productID 返回未定義,盡管收據 object 沒問題,並且有一個未定義的 productId 子項。 它應該完美地返回我'lite_plan',但它返回未定義?

  initIAP = () => {


RNIap.initConnection().then(() => {


  RNIap.flushFailedPurchasesCachedAsPendingAndroid().catch(() => {

  }).then(() => {

    console.log('open listener')
    
    this.purchaseUpdateSubscription = purchaseUpdatedListener((purchase: InAppPurchase | SubscriptionPurchase | ProductPurchase ) => {

      console.log('purchaseUpdatedListener', purchase);
      var receipt = purchase.transactionReceipt;
      if (receipt) {


       console.log('receipt >> ' + receipt)

       console.log('product id >> ' + receipt.productId)


         AsyncStorage.setItem('activeplan_local', receipt.productId).then(() => { 



.....

控制台 output:

receipt >> {"orderId":"****","packageName":"com.****","productId":"lite_plan","purchaseTime":1617193622753,"purchaseState":0,"purchaseToken":"*****","autoRenewing":true,"acknowledged":false}


product id >> undefined

你能告訴我這個問題嗎?

謝謝你的支持

你可以試試這個——

this.purchaseUpdateSubscription = purchaseUpdatedListener(async (purchase: InAppPurchase | SubscriptionPurchase | ProductPurchase ) => {

      console.log('purchaseUpdatedListener', purchase);
      // use const instead of var
      const receipt = await purchase.transactionReceipt;
      if (receipt) {

       console.log('receipt >> ' + receipt)

       console.log('product id >> ' + receipt.productId)

         AsyncStorage.setItem('activeplan_local', receipt.productId).then(() => { 

找到了這樣的解決方案:

    var receiptObject = JSON.parse(receipt)
    var productId = JSON.parse(receipt).productId

仍然無法弄清楚為什么我需要解析收據 object。 它已經是 object。 奇怪但以這種方式工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM