【问题标题】:Expo in-app-purchases: ReferenceError: Can't find variable: connectAsyncExpo in-app-purchases:ReferenceError:找不到变量:connectAsync
【发布时间】:2020-02-03 02:56:48
【问题描述】:

我正在尝试跟随世博会的in app purchases documentation.。我有 android 开发者帐户,并在该帐户上为同一个应用程序设置了我的应用程序内购买。我只是想开始使用文档并立即收到错误。

如果您查看文档,我只是想输入第一个示例中的内容。我在我的应用程序的渲染方法的开头调用了这个函数。

import * as InAppPurchases from 'expo-in-app-purchases';
...

const getHistory = async function(){ 
  const history = await connectAsync();
    if (history.responseCode === IAPResponseCode.OK) {
      history.results.forEach(result => {
        console.log(result)
      });
    }
}

我收到这些错误

[Unhandled promise rejection: ReferenceError: Can't find variable: connectAsync]
* App.js:57:19 in getHistory
- node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
- node_modules/regenerator-runtime/runtime.js:271:30 in invoke
- node_modules/regenerator-runtime/runtime.js:45:44 in tryCatch
- node_modules/regenerator-runtime/runtime.js:135:28 in invoke
- node_modules/regenerator-runtime/runtime.js:170:17 in Promise$argument_0
- node_modules/promise/setimmediate/core.js:45:7 in tryCallTwo

...

【问题讨论】:

    标签: android reactjs react-native in-app-purchase expo


    【解决方案1】:

    您需要先导入应用内购买模块。像这样:

    import * as InAppPurchases from 'expo-in-app-purchases';
    
    const getHistory = async function(){ 
      const history = await InAppPurchases.connectAsync();
        if (history.responseCode === IAPResponseCode.OK) {
          history.results.forEach(result => {
            console.log(result)
          });
        }
    }
    

    另外,请从您链接的文档中注意这一点:

    在继续之前,您必须确保已安装并配置了 react-native-unimodules 包。

    还有这个:

    请注意,应用内购买需要物理设备同时在两个平台上运行,因此无法在模拟器上进行测试。

    【讨论】:

    • 对不起,这是我的代码,我只是没有在我的示例中包含该部分
    • 请也分享那部分。
    • 所以,您仍然没有从您导入的InAppPurchases 中引用connectAsync。试试我的代码。
    • 要么使用我的代码,要么像这样导入它:import { connectAsync } from 'expo-in-app-purchases';。但我建议使用我的代码,因为您可能还想使用模块中的其他方法。
    • [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_ExpoInAppPurchases.default.connectAsync')] 出现不同的错误
    【解决方案2】:

    自从你使用:

    import * as InAppPurchases from 'expo-in-app-purchases';
    

    这意味着您的代码应如下所示: ...

    const getHistory = async function(){ 
      const history = await InAppPurchases.connectAsync();
        if (history.responseCode === InAppPurchases.IAPResponseCode.OK) {
          history.results.forEach(result => {
            console.log(result)
          });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-23
      • 2023-01-19
      • 2021-03-20
      • 2021-02-22
      • 2021-05-02
      • 2021-03-18
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      相关资源
      最近更新 更多