为什么App.js中的异步函数在初始化时返回未定义

时间:2019-05-21 19:02:04

标签: react-native

在我的本机0.59.5 public HeaderNavButton = [ { text: "main", link: "/main", id: "header-nav-button-main", class: "btn btn-primary font-weight-bold", }, { text: "news", link: "/news", id: "header-nav-button-news", class: "btn btn-primary font-weight-bold", }, { text: "entertainment", link: "/entertainment", id: "header-nav-button-entertainment", class: "btn btn-primary font-weight-bold", }, { text: "sports", link: "/sports", id: "header-nav-button-sports", class: "btn btn-primary font-weight-bold", }, { text: "business", link: "/business", id: "header-nav-button-business", class: "btn btn-primary font-weight-bold", }, { text: "society", link: "/society", id: "header-nav-button-society", class: "btn btn-primary font-weight-bold", }, { text: "wellness", link: "/wellness", id: "header-nav-button-wellness", class: "btn btn-primary font-weight-bold", }, { text: "food", link: "/food", id: "header-nav-button-food", class: "btn btn-primary font-weight-bold", }, { text: "travel", link: "/travel", id: "header-nav-button-travel", class: "btn btn-primary font-weight-bold", }, { text: "autos", link: "/autos", id: "header-nav-button-autos", class: "btn btn-primary font-weight-bold", }, { text: "media", link: "/media", id: "header-nav-button-media", class: "btn btn-primary font-weight-bold", }, 中有一个异步方法检索jwt令牌。该方法更像是包装方法,并在App.js之后被调用:

App.js

初始化后未定义jwt令牌:

....
async function getToken() {
  try {
    const result = await helper.getJwtToken();
    console.log("secure store : ", result);
    if (!result) this.props.navigation.navigate("Signup");
    return result;
  } catch(err) {
    console.log("Error in retrieve jwt token : ", err.message);
    return {};
  };      

};

//socket.io
const result = getToken();
let jwt = result.password;
console.log("jwt token in App.js : ", jwt);
const socket = io(GLOBAL.BASE_URL + `?token=${jwt}`, {
  //const socket = io(GLOBAL.BASE_URL, {
    transports: ['websocket'],
    jsonp: false
  });
console.log("socket id in App.js : ", socket.id);

但是在初始化后启动初始组件时,类似的代码(检索jwt令牌)可以正常工作,并且成功检索到存储的jwt令牌,socket.io也是如此。如何使此05-20 22:47:31.660 23027 23027 D ReactNative: ReactInstanceManager.ctor() 05-20 22:47:31.904 23027 23027 D ReactNative: ReactInstanceManager.createReactContextInBackground() 05-20 22:47:31.905 23027 23027 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner() 05-20 22:47:43.156 23027 23027 D ReactNative: ReactInstanceManager.onJSBundleLoadedFromServer() 05-20 22:47:43.206 23027 23027 D ReactNative: ReactInstanceManager.recreateReactContextInBackground() 05-20 22:47:43.207 23027 23027 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread() 05-20 22:47:43.346 23027 23096 D ReactNative: ReactInstanceManager.createReactContext() 05-20 22:47:43.525 23027 23096 D ReactNative: Initializing React Xplat Bridge. 05-20 22:47:43.545 23027 23096 D ReactNative: Initializing React Xplat Bridge before initializeBridge 05-20 22:47:43.572 23027 23096 D ReactNative: Initializing React Xplat Bridge after initializeBridge 05-20 22:47:43.572 23027 23096 D ReactNative: CatalystInstanceImpl.runJSBundle() 05-20 22:47:43.575 23027 23101 D ReactNative: ReactInstanceManager.setupReactContext() 05-20 22:47:43.577 23027 23101 D ReactNative: CatalystInstanceImpl.initialize() 05-20 22:47:43.593 23027 23101 D ReactNative: ReactInstanceManager.attachRootViewToInstance() 05-20 22:47:49.001 23027 23100 I ReactNativeJS: 'secure store : ', '' 05-20 22:47:49.002 23027 23100 I ReactNativeJS: 'Error in retrieve jwt token : ', 'undefined is not an object (evaluating \'this.props.navigation\')' 05-20 22:47:49.049 23027 23100 I ReactNativeJS: 'socket id in App.js : ', undefined 代码在async getToken中工作以进行初始化?

1 个答案:

答案 0 :(得分:1)

问题在于您不必等待getToken()函数完成。

在调用getToken()时尝试使用 await ,如下所示:

//socket.io
const result = await getToken();
let jwt = result.password;
console.log("jwt token in App.js : ", jwt);
const socket = io(GLOBAL.BASE_URL + `?token=${jwt}`, {
  //const socket = io(GLOBAL.BASE_URL, {
    transports: ['websocket'],
    jsonp: false
  });
console.log("socket id in App.js : ", socket.id);