React Native:RNIap.getPurchaseHistory然后无限运行

问题描述

该项目位于此Github Repository。包含代码文件位于components/Soundboard.js


代码以前可以运行,但是现在看来,承诺将永远运行。看来解析功能和拒绝功能都没有执行,因为如果我取消注释下面所有的注释行并调用功能askForPurchase(),则唯一打印到控制台的内容就是

  • "_40": 0,"_55": {"_40": 0,"_55": null,"_65": 0,"_72": null},"_65": 3,"_72": null}行中看起来像console.log(RNIap.getPurchaseHistory())的对象
  • 然后是单词end

buyProduct()函数也不再初始化IAP。

const buyProduct = function(){
    RNIap.requestPurchase("1985162691",false).then(purchase => {
        store.dispatch(setPurchases(purchase))
        await RNIap.finishTransaction(purchase,false)    //developerPayloadAndroid?: string   Should I use this argument? I don't get how to use it
    }).catch((error) => {
        console.log(error.message);
    })
}

const askForPurchase = function(){
    if (!store.getState().purchase){
        //console.log(RNIap.getPurchaseHistory())
        RNIap.getPurchaseHistory().then(purchase => {
            //console.log(`test1`)
            store.dispatch(setPurchases(purchase))
            if (purchase.length == 0){
                //console.log(`test if`)
                buyProduct()
            }else{
                //console.log(`test else`)
                RNIap.getAvailablePurchases()
            }
        },reason => {
            console.log(reason)
        })
        //console.log(`end`)
    }
}

额外

代码在几个月前开始工作,我什至从那时起提交了一个commit( 1b9cb81f229680e173ce910892dddedc632c1651,评论:“使封条图片更具卡通色彩” )进行测试。拉出此提交后,我删除了我的node_modules和pod,并清理了我的构建文件夹,但是askForPurchase()buyProduct()函数在该提交中也不再起作用。

我正在运行ios 13.6.1的真实iPhone SE上进行测试


如果您需要测试它,我创建了一个沙箱测试器,但我认为您不需要它

email: rniapsandBox@gmail.com
pw: Somepassword1

解决方法

您好@Sam问题是异步等待问题,他们无法获取价值,因为他们没有在获取数据之前就没有等待获取数据,而是在没有数据的情况下触发了数据,并且正在返回promise,因此您必须使用async function >

所以您的代码就像

const buyProduct = async()=>{
   await RNIap.requestPurchase("1985162691",false).then(purchase => {
    store.dispatch(setPurchases(purchase))
    await RNIap.finishTransaction(purchase,false)    //developerPayloadAndroid?: string   Should I use this argument? I don't get how to use it
}).catch((error) => {
    console.log(error.message);
})}

const askForPurchase = async()=>{
if (!store.getState().purchase){
    //console.log(await RNIap.getPurchaseHistory())
   await RNIap.getPurchaseHistory().then(purchase => {
        //console.log(`test1`)
        store.dispatch(setPurchases(purchase))
        if (purchase.length == 0){
            //console.log(`test if`)
            buyProduct()
        }else{
            //console.log(`test else`)
            RNIap.getAvailablePurchases()
        }
    },reason => {
        console.log(reason)
    })
    //console.log(`end`)
}}
,

您需要从以下位置更改

console.log(RNIap.getPurchaseHistory())

console.log(await RNIap.getPurchaseHistory())