【问题标题】:React-Native Child of object returns undefined despite it exists对象的 React-Native Child 返回 undefined 尽管它存在
【发布时间】:2021-03-31 12:40:35
【问题描述】:

我有一个非常基本但令人兴奋的问题,我无法弄清楚。这是这样的:

问题的简短总结

我从 IAP 获得了 receipt 对象,但无法获得 productId 孩子。它返回 undefined ?怎么会发生?收据.productID 返回未定义,尽管收据对象没问题,并且有一个未定义的 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(() => { 



.....

控制台输出:

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


product id >> undefined

你能告诉我这个问题吗?

感谢您的支持

【问题讨论】:

    标签: javascript react-native react-native-iap


    【解决方案1】:

    你可以试试这个-

    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(() => { 
    

    【讨论】:

    • 感谢您的回答,但还是一样。我也尝试过 if 和receipt2.productId 中的const receive2 =receipt 并且仍然相同。有多奇怪是对的?它更像是 javascript 问题,所以我编辑了标题。
    【解决方案2】:

    找到这样的解决方案:

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

    仍然无法弄清楚为什么我需要解析收据对象。它已经是一个对象。奇怪但以这种方式工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多