简体   繁体   English

JSON 解析错误:意外的标识符“对象”

[英]JSON parse error: Unexpected identifier “object”

I am saving my firestore data which is array in AsyncStorage.我正在保存我的 Firestore 数据,该数据是 AsyncStorage 中的数组。 And again i am getting it.我又明白了。 Here is my code:这是我的代码:

AsyncStorage.setItem('Users', JSON.stringify(doc.data().Items));

So in firestore my Items is an array所以在 firestore 我的 Items 是一个array

While getting that array i am getting the mentioned error.在获取该数组时,我收到了上述错误。

async componentWillMount() {
    const myArray = await AsyncStorage.getItem('Users');
    alert(JSON.parse(myArray))
    if (myArray != null || myArray != undefined || myArray != '') {
            this.setState({ UserArray: JSON.parse(myArray) })
    }
}

Update:更新:

 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>
    );
}

My render is straight forward.我的渲染是直截了当的。 But if i set the state for my UserArray.但是,如果我为我的 UserArray 设置 state。 Then i am getting this error然后我收到这个错误

AsyncStorage.getItem('Users') is a asynchronous function, so you must have await | async AsyncStorage.getItem('Users')是异步的 function,所以你必须有await | async await | async in your function.在你的 function 中await | async

You can try:你可以试试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM