![](/img/trans.png)
[英]SyntaxError: JSON Parse error: Unexpected identifier “object”
[英]JSON parse error: Unexpected identifier “object”
我正在保存我的 Firestore 数据,该数据是 AsyncStorage 中的数组。 我又明白了。 这是我的代码:
AsyncStorage.setItem('Users', JSON.stringify(doc.data().Items));
所以在 firestore 我的 Items 是一个array
在获取该数组时,我收到了上述错误。
async componentWillMount() {
const myArray = await AsyncStorage.getItem('Users');
alert(JSON.parse(myArray))
if (myArray != null || myArray != undefined || myArray != '') {
this.setState({ UserArray: JSON.parse(myArray) })
}
}
更新:
renderPost = post => {
return (
<TouchableWithoutFeedback onPress={ () => this.passDateToNextScreen(post)}>
<Spinner
visible={this.state.showLoader}
text="Uploading ..."
textStyle={styles.spinnerTextStyle}
/>
<View style={styles.feedItem}>
<Feather name="book" style={styles.avatar} size={30}/>
<View style={{ flex: 1 }}>
<View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
<View>
<Text style={styles.name}>{post.RecipeName}</Text>
<Text style={styles.timestamp}>{post.RecipeFullTimeTakenToCook} | {post.RecipeCategory}</Text>
</View>
</View>
</View>
<Feather name="chevron-right" style={{color: "#808080", alignSelf: 'center'}} size={20}/>
</View>
</TouchableWithoutFeedback>
);
};
render() {
return (
<View style={styles.container}>
{
this.state.UserArray.length ?
(<FlatList
style={styles.feed}
data={this.state.UserArray}
renderItem={({ item }) => this.renderPost(item)}
showsVerticalScrollIndicator={false}
></FlatList>) :
(
<View style={{ width: '70%', alignSelf: 'center',justifyContent: 'center' , alignItems: 'center', marginTop: '20%'}}>
<Feather name="smile" color="#009387" size={40}/>
<Text style={{paddingTop: 20, textAlign: 'center', fontSize: 15, fontWeight: 'bold', color: '#A9A9A9'}}>Hey folk, You dont' have any recipe list. Please tap on edit button to write your recipe book list</Text>
</View>
)
}
</View>
);
}
我的渲染是直截了当的。 但是,如果我为我的 UserArray 设置 state。 然后我收到这个错误
AsyncStorage.getItem('Users')
是异步的 function,所以你必须有await | async
在你的 function 中await | async
。
你可以试试:
async componentWillMount() {
const myArray = await AsyncStorage.getItem('Users');
alert(JSON.parse(myArray))
if (myArray != null || myArray != undefined || myArray != '') {
this.setState({ UserArray: JSON.parse(myArray) })
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.