Expo in-app-purchases: ReferenceError: Can't find variable: connectAsync

和自甴很熟 提交于 2021-01-29 09:22:31

问题


I'm trying to follow along with expo's in app purchases documentation.. I have the android developer account and set up my in-app-purchase on that account for the same app. I'm just trying to get started with the documentation and receive an error immediately.

If you look at the documentation, I'm just trying to enter in what's in the first example. I call this function at the beginning of the render method of my app.

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)
      });
    }
}

And I receive these errors

[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

...

回答1:


You need to import in-app purchases module first. Like this:

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)
      });
    }
}

Also, note this from the documentation you linked:

You must ensure that you have installed and configured the react-native-unimodules package before continuing.

and this:

Note that in-app purchases require physical devices to work on both platforms and therefore cannot be tested on simulators.




回答2:


Since you used:

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

That means your code should look like: ...

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


来源:https://stackoverflow.com/questions/60032575/expo-in-app-purchases-referenceerror-cant-find-variable-connectasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!