如果我使用等待,React-Native应用程序将无法正常工作

时间:2018-12-06 11:01:21

标签: javascript react-native google-cloud-firestore

这是我第一次使用react-native开发Android应用程序。 当我从firestore检索一些数据时,我尝试使用async / await,但是当我添加await一词时,应用程序崩溃了,它将不再启动。 如您在package.json文件中所见,我使用react-native-firebase库。 这是我组件中不起作用的部分:

componentDidMount() {
  this.getAccountFromFirestore(this.props.user);
}

getAccountFromFirestore = async user => {
  const accounts = await firebase
   .firestore()
   .collection('accounts')
   .get();

  this.setState({ loading: false }); 
};

这是我的package.json,如果有用的话

{
  "name": "myapp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "ios": "react-native run-ios",
    "android": "react-native run-android"
  },
  "dependencies": {
    "native-base": "^2.8.1",
    "react": "16.6.1",
    "react-native": "0.57.6",
    "react-native-firebase": "^5.1.1",
    "react-native-navigation": "^1.1.493",
    "react-native-vector-icons": "^6.1.0",
    "react-redux": "^5.1.1",
    "redux": "^4.0.1"
  },
  "devDependencies": {
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.49.2",
    "react-test-renderer": "16.6.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

3 个答案:

答案 0 :(得分:0)

您可以通过Promise来实现:

 firebase
 .firestore()
 .collection('accounts')
  .get()
  .then(accounts => {
   console.log ('Use accounts variable as per your requirement,'accounts)
  })
  .catch(error => {console.error(error)})

答案 1 :(得分:0)

我不知道它是您的React Native还是您正在使用 Node 6 。但是据我所知,在节点8或更高版本上仅支持FROM node:10-alpine RUN npm install -g nodemon RUN mkdir -p /action WORKDIR / COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh VOLUME /action/functions/node_modules EXPOSE 9229 ENTRYPOINT ["sh","/entrypoint.sh"] 。因此,最好的选择仍然是使用#!/bin/sh cd /action/functions npm install nodemon --inspect=0.0.0.0:9229 start.js -L 来解决此问题。例如:

async/await

此外,在使用Promises时,请不要忘记componentDidMount() { this.getAccountFromFirestore(this.props.user); } getAccountFromFirestore = user => { firebase .firestore() .collection('accounts') .get() .then(accounts => { let data = accounts.docs.map(account => account.data()); console.log(data); this.setState({ loading: false }); }) .catch(err => console.log(err)); }; 块:

async/await

答案 2 :(得分:0)

选项a)继续使用诺言 选项b)迁移该RN,如果可以使用异步/等待,则您的代码可能太旧而无法进入商店。 选项c)Babel ...