我正在使用应用内购买,如果由于某种原因我无法从 Google 或 iOS 应用商店检索产品信息,我想将 UI 更改为“不可用”。
ionViewDidEnter() {
this.platform.ready().then(async () => {
firebase.auth().onAuthStateChanged(async user => {
this.currentUser = user;
});
this.unavailable = await this.setupProducts();
console.log('available', this.unavailable);
});
}
setupProducts(): Promise<boolean> {
let productWWHS: string;
let productISA: string;
if (this.platform.is('ios')) {
productWWHS = 'prodID';
productISA = 'prodID';
} else if (this.platform.is('android')) {
productWWHS = 'prodID';
productISA = 'prodID';
}
this.inAppPurchase.ready(() => {
this.products.push(this.inAppPurchase.get(productWWHS));
this.products.push(this.inAppPurchase.get(productISA));
if (!this.products[0]) {
return true;
}
});
return false;
}
我在这个方法中做错了什么,它有错误 类型“boolean”不可分配给类型“Promise”
我想以某种方式断言 inAppPurchase.get() 已经返回了一些东西,但它没有返回一个承诺。
有更好的方法吗?
如有任何帮助,我们将不胜感激。
要修复输入错误,您需要将函数定义为
async
:
async setupProducts(): Promise<boolean> {
...
return false;
}
请注意,
true
中的 this.inAppPurchase.ready(() => {...})
值不会从 setupProducts()
返回。它将从匿名函数() => {...}
返回,不会影响任何东西。
你可能需要类似的东西
async setupProducts(): Promise<boolean> {
...
await this.inAppPurchase;
this.products.push(this.inAppPurchase.get(productWWHS));
this.products.push(this.inAppPurchase.get(productISA));
if (!this.products[0]) {
return true;
}
return false;
}
如果
()
是函数而不是 getter,请不要忘记 this.inAppPurchase
。
“类型‘boolean’不能分配给类型‘Promise’”,通常发生在当你有一个函数或代码期望返回 Promise,但你返回一个普通的布尔值时。这是在 TypeScript 中使用异步代码时的常见错误。
function someAsyncFunction(): Promise<boolean> {
return Promise.resolve(true); // Replace 'true' with your boolean result
}