我在 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])
但在控制台中我看到未定义
我该如何解决这个问题?
如果您需要更多信息,请告诉我!
为此,我建议您记录
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]);
如果这有帮助,请告诉我。