You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native内购报错:TypeError: 'in'右操作数非对象

React Native IAP 订阅购买报错:TypeError: right operand of 'in' is not an object

问题详情

我使用react-native-iap库为React Native应用实现内购功能,已成功从Google Play商店获取订阅详情,但发起购买时遇到错误:TypeError: right operand of 'in' is not an object。

获取订阅详情代码

useEffect(() => {
  (async () => {
    try {
      const subs = await Iap.getSubscriptions([selectedPlan.google_pay_id]);
      if (subs && subs.length > 0) {
        console.log('Subscriptions response: ', subs);
        setGoogleSub(subs[0]);
      }
    } catch (error) {
      Sentry.captureMessage('Failed to get Google subscriptions', error);
      console.error(error);
    }
  })();
}, [selectedPlan]);

const requestGoogleSubscription = async () => {
  try {
    setIsLoading(true);
    console.log('requestGoogleSubscription: googleSub', googleSub);
    if (googleSub && typeof googleSub === 'object' && googleSub.productId) {
      console.log('requestGoogleSubscription: initiating purchase');
      const purchase = await Iap.requestSubscription(googleSub.productId);
    } else {
      console.warn('No valid Google subscription details available');
    }
  } catch (error) {
    Sentry.captureMessage('Failed Google subscription', error);
    console.error(error);
  } finally {
    setIsLoading(false);
  }
};

googleSub 示例数据

selectedPlan对象包含google_pay_id(对应Google Play商店中的订阅产品ID),googleSub是Iap.getSubscriptions()返回数组的第一个对象,日志示例如下:

{
  "description": "test123",
  "name": "Weekly Subscription 3",
  "platform": "android",
  "productId": "weekly_subscription_3",
  "productType": "subs",
  "subscriptionOfferDetails": [{
    "basePlanId": "weekly-subscription-3",
    "offerId": null,
    "offerTags": [],
    "offerToken": "Auj",
    "pricingPhases": [{}]
  }],
  "title": "Weekly Subscription 3"
}

解决方案

  • 适配requestSubscription传参格式
    部分版本的react-native-iap要求订阅购买时传入包含productId和subscriptionOfferToken的对象,而非直接传productId。可修改购买代码:

    const offerToken = googleSub.subscriptionOfferDetails[0].offerToken;
    const purchase = await Iap.requestSubscription({
      productId: googleSub.productId,
      subscriptionOfferToken: offerToken
    });
    
  • 校验订阅数据完整性
    增加对subscriptionOfferDetails的有效性校验,避免空数据导致解析异常:

    if (googleSub.subscriptionOfferDetails?.length > 0 && googleSub.subscriptionOfferDetails[0].offerToken) {
      // 发起购买逻辑
    } else {
      console.warn('Invalid subscription offer details');
    }
    
  • 检查库版本兼容性
    该报错常出现在版本不匹配场景,建议升级react-native-iap到最新稳定版,或回退到已知兼容的版本(如v12.x系列),同时确保Android端Google Play Billing Library版本与库依赖一致。


内容的提问来源于stack exchange,提问作者NooBskie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:12:51