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

React Native IAP调用getSubscriptions报"skus is required"错误求助

解决React Native IAP调用getSubscriptions报"skus is required"错误的方案

问题根源

  1. 参数格式错误:getSubscriptions函数要求传入包含skus字段的对象,而非直接传SKU数组。
  2. 未处理Promise异常:调用异步函数未捕获错误,导致出现未处理的Promise拒绝警告。
  3. 潜在变量位置问题:items定义在import语句前,虽语法合法但易引发变量提升相关问题。
  4. 未验证连接状态:未确保IAP连接成功(connected为true)就调用获取订阅的方法。

修复步骤

  • 修正getSubscriptions调用参数:将getSubscriptions(items)改为getSubscriptions({ skus: items }),匹配函数参数要求。
  • 调整变量位置:将items移至import语句之后,组件定义之前,避免变量提升问题。
  • 添加错误捕获:在调用getSubscriptions时用try/catch处理异常,避免未捕获的Promise拒绝。
  • 验证连接状态:调用前检查connected是否为true,确保IAP连接已初始化完成。

修改后的完整代码

import React, { useEffect } from 'react';
import { View, Text, Button, Platform } from 'react-native';
import { requestPurchase, useIAP } from 'react-native-iap';

// 调整items位置到import之后
const items = Platform.select({
  ios: [
    'dev.products.gas',
    'dev.products.premium',
    'dev.products.gold_monthly',
    'dev.products.gold_yearly',
  ],
  android: [
    'dev.products.gas',
    'dev.products.premium',
    'dev.products.gold_monthly',
    'dev.products.gold_yearly',
  ],
});

const ParchesCode = () => {
  const {
    connected,
    subscriptions,
    currentPurchaseError,
    currentPurchase,
    finishTransaction,
    getSubscriptions,
  } = useIAP();

  const handlePurchase = async (sku) => {
    await requestPurchase({ sku });
  };

  const fetchSubscriptions = async () => {
    // 检查连接状态
    if (!connected) {
      console.log('IAP连接未初始化');
      return;
    }
    try {
      // 修正参数格式
      await getSubscriptions({ skus: items });
    } catch (error) {
      console.error('获取订阅失败:', error);
    }
  };

  useEffect(() => {
    if (currentPurchaseError) {
      console.error('购买错误:', currentPurchaseError);
    }
  }, [currentPurchaseError]);

  useEffect(() => {
    if (currentPurchase) {
      // 处理购买完成逻辑,比如finishTransaction
      finishTransaction({ purchase: currentPurchase });
    }
  }, [currentPurchase]);

  return (
    <View style={{ flex: 1, padding: 20 }}>
      <Button
        title="获取订阅商品"
        onPress={fetchSubscriptions}
      />

      {subscriptions.length > 0 ? (
        subscriptions.map((product) => (
          <View key={product.productId} style={{ marginVertical: 10, padding: 10, borderWidth: 1 }}>
            <Text>{product.productId}</Text>
            <Button
              title="购买"
              onPress={() => handlePurchase(product.productId)}
            />
          </View>
        ))
      ) : (
        <Text>暂无订阅商品</Text>
      )}
      <Text style={{ marginTop: 20 }}>连接状态: {JSON.stringify(connected)}</Text>
    </View>
  );
};

export default ParchesCode;

额外注意事项

  • 确保Play商店后台的订阅商品ID与代码中的完全一致,且应用已关联正确的开发者账号和支付配置。
  • 测试时需使用发布版签名的APK/AAB,或在Play控制台的内部测试渠道进行测试,模拟器可能无法正常获取真实商品信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:20:42