繁体   English   中英

类型“boolean”不可分配给类型“Promise”<boolean> '</boolean>

[英]Type 'boolean' is not assignable to type 'Promise<boolean>'

我正在使用应用内购买,如果由于某种原因我无法从 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' is not assignable to type 'Promise'

我想以某种方式断言 inAppPurchase.get() 返回了一些东西,但它没有返回 promise。

有一个更好的方法吗?

任何帮助,将不胜感激。

要修复输入错误,您需要将 function 定义为async

async setupProducts(): Promise<boolean> {
...
return false;
}

请注意,来自this.inAppPurchase.ready(() => {...})true值不会从setupProducts()返回。 它将从匿名 function () => {...}返回并且不会影响任何东西。

你可能需要类似的东西

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

不要忘记()如果this.inAppPurchase是 function 而不是 getter。

我正在使用 inApp 购买,如果由于某种原因我无法从 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() 已经返回了一些东西,但它没有返回 promise。

有一个更好的方法吗?

任何帮助,将不胜感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM