为什么我无法使用react-native-iap 获得订阅

问题描述 投票:0回答:1

我在 App Store Connect 上创建了一个订阅

在此输入图片描述

并尝试在我的代码中获取此订阅,如下所示

  const { connected, getSubscriptions } = useIAP()

  const subSkus = ['Basic']

  const handleGetSubscriptions = async () => {
    try {
      if (connected) {
        const subs = await getSubscriptions({ skus: subSkus })

        console.log(subs, 'subs')
      }
    } catch (error) {
      console.error('Error in handleGetSubscriptions:', error)
    }
  }

  React.useEffect(() => {
    handleGetSubscriptions()
  }, [connected])

但在控制台中我看到未定义

我该如何解决这个问题?

如果您需要更多信息,请告诉我!

react-native in-app-purchase react-native-iap
1个回答
0
投票

为此,我建议您记录

getSubscriptions
调用期间发生的任何错误,以了解为什么 subs 可能是
undefined
,因此请用给定的代码替换您的代码。

const { connected, getSubscriptions } = useIAP();
const subSkus = ['Basic'];

const handleGetSubscriptions = async () => {
  try {
    if ( connected ) {
      const subs = await getSubscriptions({ skus: subSkus });

      console.log('Subscription response:', subs); // Log the entire response to see what's returned

      if ( subs ) {
        console.log( 'Subscriptions fetched successfully:', subs );
      } else {
        console.log( 'No subscriptions fetched or empty response' );
      }
    } else {
      console.log( 'Not connected to IAP service' );
    }
  } catch ( error ) {
    console.error( 'Error in handleGetSubscriptions:', error );
  }
};

React.useEffect(() => {
  handleGetSubscriptions();
}, [connected]);

如果这有帮助,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.