Node.js 正在尝试检索异步/等待的结果

Node.js 正在尝试检索异步/等待的结果,node.js,reactjs,async-await,Node.js,Reactjs,Async Await,我试图从带有async/await的承诺中检索结果,但收到以下错误: 544:1未捕获错误:模块生成失败:语法错误:wait是保留字(30:23) 这是我的密码: 从“React”导入React; 从“./头”导入头; 从“./Feed”导入提要; 从“lodash”进口; const Cosmic=require('cosmicjs')(); 导出默认类,该类扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ 目的地:“”, 帖子:'', 全局:“”,

我试图从带有async/await的承诺中检索结果,但收到以下错误:

544:1未捕获错误:模块生成失败:语法错误:wait是保留字(30:23)

这是我的密码:

从“React”导入React;
从“./头”导入头;
从“./Feed”导入提要;
从“lodash”进口;
const Cosmic=require('cosmicjs')();
导出默认类,该类扩展React.Component{
建造师(道具){
超级(道具);
此.state={
目的地:“”,
帖子:'',
全局:“”,
}
}
render(){
const bucket=Cosmic.bucket({
鼻涕虫:“她去哪了”,
读取密钥:“”,
写入密钥:“”
});
const retrieveBucket=async()=>{
试一试{
让结果=bucket.getBucket()
返回结果
}捕捉(错误){
console.log(错误)
}
}
const result=await retrieveBucket()
console.log(结果)
this.setState(()=>{
返回{
目标:u.filter(result.bucket.objects,{type_slug:'destinations'}),
posts:u.filter(result.bucket.objects,{type_slug:'posts'}),
globals:u.filter(result.bucket.objects,{type_slug:'globals'})
}
});
返回(
);
}

}
您只能在异步函数中使用
wait

您也可以使用
。然后

从“React”导入React; 从“./头”导入头; 从“./Feed”导入提要; 从“lodash”进口; const Cosmic=require('cosmicjs')()

导出默认类,其中她扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
目的地:“”,
帖子:'',
全局:“”,
}
}
检索桶=()=>{
const bucket=Cosmic.bucket({
鼻涕虫:“她去哪了”,
读取密钥:“”,
写入密钥:“”
});
试一试{
bucket.getBucket().then(结果=>{
this.setState(()=>{
返回{
目标:u.filter(result.bucket.objects,{type_slug:'destinations'}),
posts:u.filter(result.bucket.objects,{type_slug:'posts'}),
globals:u.filter(result.bucket.objects,{type_slug:'globals'})
}
});
});
}捕捉(错误){
console.log(错误)
}
}
render(){
这个.retrievebucket();
返回(
);
}
}

await
只能在使用
async
声明的函数内部使用。因此,您应该在这个函数中使用wait来获取数据并设置状态。 此外,您的代码也不是最优的,因为它将尝试在组件的每个重新加载程序上接收数据。更好地构造类并使用生命周期方法,如
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>
        );
    }
}```
导出默认类,其中她扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
目的地:“”,
帖子:'',
全局:“”,
}
}
componentDidMount(){
retrieveBucket();
}
retrieveBucket=async()=>{
const bucket=Cosmic.bucket({
鼻涕虫:“她去哪了”,
读取密钥:“”,
写入密钥:“”
});
试一试{
让结果=等待bucket.getBucket()
console.log(结果)
这是我的国家({
目标:u.filter(result.bucket.objects,{type_slug:'destinations'}),
posts:u.filter(result.bucket.objects,{type_slug:'posts'}),
globals:u.filter(result.bucket.objects,{type_slug:'globals'})
});
}捕捉(错误){
console.log(错误)
}
}
render(){
返回(
);
}
}```

井的可能副本我相信您只能在
async
功能中使用
wait
。。。
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>
        );
    }
}```