我正在尝试使用async / await从诺言中检索结果,但是收到以下错误:
544:1未捕获的错误:模块构建失败:语法错误:等待是保留字(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>
);
}
}
只有在返回结果并在实际的异步函数中设置状态后,我才能使以上代码运行。如何在该函数之外返回承诺的结果?
谢谢!
答案 0 :(得分: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>
);
}
}```
答案 1 :(得分:0)
您只能在异步函数中使用await
。
您也可以使用.then
从'react'导入React; 从'./Header'导入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>
);
}
}