[英]Async JS function is undefined when importing into React Native Component
我想創建一個包含一些實用程序功能的JavaScript文件,以import
到一些React Native組件中。 我創建了一個文件“ PremiumStatus.js”,其中包含一些功能:
import * as InAppPurchase from 'react-native-iap';
import { AsyncStorage } from "react-native";
async function getPreviousPurchases() {
let purchases;
try {
purchases = await InAppPurchase.getPurchaseHistory();
console.log("previous purchases: ", purchases);
} catch (e) {
console.warn(e);
}
return purchases;
}
export function doSomething() {
console.log("doing something");
}
export async function hasPurchasedPremium() {
console.log("calling hasPurchasedPremium");
return true; // TODO: remove this when done testing
}
然后在類似這樣的React Native組件中使用它:
import { hasPurchasedPremium, doSomething } from "../../helpers/PremiumStatus";
...
async componentDidMount() {
doSomething();
...
const hasPurchasedPremium = await hasPurchasedPremium();
...
}
當我運行它時,我得到以下調試輸出:
05-04 20:40:33.540 29070 29819 I ReactNativeJS: doing something
05-04 20:40:33.789 29070 29819 W ReactNativeJS: Possible Unhandled Promise Rejection (id: 0):
05-04 20:40:33.789 29070 29819 W ReactNativeJS: TypeError: undefined is not a function (evaluating '_hasPurchasedPremium()')
05-04 20:40:33.789 29070 29819 W ReactNativeJS: componentDidMount$@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:69301:85
05-04 20:40:33.789 29070 29819 W ReactNativeJS: tryCatch@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16727:23
05-04 20:40:33.789 29070 29819 W ReactNativeJS: invoke@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16900:32
05-04 20:40:33.789 29070 29819 W ReactNativeJS: http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16770:30
05-04 20:40:33.789 29070 29819 W ReactNativeJS: tryCatch@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16727:23
05-04 20:40:33.789 29070 29819 W ReactNativeJS: invoke@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16803:30
05-04 20:40:33.789 29070 29819 W ReactNativeJS: http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16813:21
05-04 20:40:33.789 29070 29819 W ReactNativeJS: tryCallOne@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16056:16
05-04 20:40:33.789 29070 29819 W ReactNativeJS: http://localhost:8081/index.delta?platform=android&dev=true&minify=false:16157:27
05-04 20:40:33.789 29070 29819 W ReactNativeJS: http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2884:26
05-04 20:40:33.789 29070 29819 W ReactNativeJS: _callTimer@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2773:17
05-04 20:40:33.789 29070 29819 W ReactNativeJS: _callImmediatesPass@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2809:19
05-04 20:40:33.789 29070 29819 W ReactNativeJS: callImmediates@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:3028:33
05-04 20:40:33.789 29070 29819 W ReactNativeJS: __callImmediates@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2362:32
05-04 20:40:33.789 29070 29819 W ReactNativeJS: http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2189:34
05-04 20:40:33.789 29070 29819 W ReactNativeJS: __guardSafe@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2346:13
05-04 20:40:33.789 29070 29819 W ReactNativeJS: flushedQueue@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2188:21
05-04 20:40:33.789 29070 29819 W ReactNativeJS: flushedQueue@[native code]
05-04 20:40:33.789 29070 29819 W ReactNativeJS: invokeCallbackAndReturnFlushedQueue@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2181:33
05-04 20:40:33.789 29070 29819 W ReactNativeJS: invokeCallbackAndReturnFlushedQueue@[native code]
最有趣的一行是“ TypeError:undefined不是函數(評估'_hasPurchasedPremium()')”。
當常規函數正常工作時,為什么未定義異步函數?
它似乎與通天塔有關。 我不確定在您的本機React版本中如何設置babel,但是下面的解決方法應該起作用:
async function hasPurchasedPremium() {
console.log("calling hasPurchasedPremium");
return true; // TODO: remove this when done testing
}
export { hasPurchasedPremium }
這真是不可思議,但是在我重命名了異步函數后,錯誤消失了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.