【问题标题】:Trying to retrieve result of async/await试图检索异步/等待的结果
【发布时间】:2019-10-11 20:40:51
【问题描述】:

我正在尝试使用 async / await 从 Promise 中检索结果,但收到以下错误:

544:1 未捕获的错误:模块构建失败:SyntaxError: await 是保留字 (30:23)

这是我的代码:

import React from 'react';
import Header from './Header';
import Feed from './Feed';
import _ from 'lodash';
const Cosmic = require('cosmicjs')();

export default class WhereSheGoes extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            destinations: '',
            posts: '',
            globals: '',
        }
    }
    render() {
        const bucket = Cosmic.bucket({
            slug: 'where-she-goes',
            read_key: '',
            write_key: ''
        });
        const retrieveBucket = async () => {
            try {
                let result = bucket.getBucket()
                return result
            } catch (err) {
                console.log(err)
            }
        }
        const result = await retrieveBucket()
        console.log(result)
        this.setState (() => {
            return {
                destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}),
                posts: _.filter(result.bucket.objects, {type_slug: 'posts'}),
                globals: _.filter(result.bucket.objects, {type_slug: 'globals'})
            }
        });
        return (
            <div>
                <Header 
                    destinations={this.state.destinations}
                    posts={this.state.posts}
                    globals={this.state.globals}
                />
                <Feed
                    posts={this.state.posts}
                    destinations={this.state.destinations}
                />
            </div>
        );
    }
}

我可以让上面的代码运行,但前提是我返回结果并在实际的异步函数中设置状态。如何在该函数之外返回 promise 的结果?

谢谢!

【问题讨论】:

标签: node.js reactjs async-await


【解决方案1】:

await 只能在用async 声明的函数内部使用。所以你应该在这个函数中使用 await 来获取你的数据并设置状态。 此外,您的代码也不是最优的,因为它会尝试在每次重新渲染组件时接收数据。更好地构建您的类并使用诸如componentDidMount 之类的生命周期方法来获取数据并将其存储到状态中。然后在渲染中使用状态来显示它

export default class WhereSheGoes extends React.Component {
    constructor (props) {
        super(props);
        this.state = {
            destinations: '',
            posts: '',
            globals: '',
        }
    }

    componentDidMount() {
        retrieveBucket();
    }

    retrieveBucket = async () => {
        const bucket = Cosmic.bucket({
            slug: 'where-she-goes',
            read_key: '',
            write_key: ''
        });
       try {
          let result = await bucket.getBucket()
          console.log(result)
          this.setState ({
                destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}),
                posts: _.filter(result.bucket.objects, {type_slug: 'posts'}),
                globals: _.filter(result.bucket.objects, {type_slug: 'globals'})
            });
       } catch (err) {
            console.log(err)
       }
    }

    render() {
        return (
            <div>
                <Header 
                    destinations={this.state.destinations}
                    posts={this.state.posts}
                    globals={this.state.globals}
                />
                <Feed
                    posts={this.state.posts}
                    destinations={this.state.destinations}
                />
            </div>
        );
    }
}```

【讨论】:

    【解决方案2】:

    您只能在异步函数中使用await

    你也可以使用.then

    从“反应”导入反应; 从'./Header'导入标题; 从'./Feed'导入Feed; 从'lodash'导入_; const Cosmic = require('cosmicjs')();

    export default class WhereSheGoes extends React.Component {
        constructor (props) {
            super(props);
            this.state = {
                destinations: '',
                posts: '',
                globals: '',
            }
        }
    
        retriveBucket = () => {
          const bucket = Cosmic.bucket({
              slug: 'where-she-goes',
              read_key: '',
              write_key: ''
          });
    
          try {
            bucket.getBucket().then(result => {
              this.setState (() => {
                  return {
                      destinations: _.filter(result.bucket.objects, {type_slug: 'destinations'}),
                      posts: _.filter(result.bucket.objects, {type_slug: 'posts'}),
                      globals: _.filter(result.bucket.objects, {type_slug: 'globals'})
                  }
              });
            });
          } catch (err) {
              console.log(err)
          }
        }
    
        render() {
            this.retriveBucket();
    
            return (
                <div>
                    <Header 
                        destinations={this.state.destinations}
                        posts={this.state.posts}
                        globals={this.state.globals}
                    />
                    <Feed
                        posts={this.state.posts}
                        destinations={this.state.destinations}
                    />
                </div>
            );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      相关资源
      最近更新 更多