簡體   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