【问题标题】:JSON parse error: Unexpected identifier "object"JSON 解析错误:意外的标识符“对象”
【发布时间】:2020-06-12 10:44:49
【问题描述】:

我正在保存我的 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 设置状态。然后我收到此错误

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    AsyncStorage.getItem('Users') 是一个异步函数,所以你的函数中必须有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) })
        }
    }
    

    【讨论】:

    • 但我收到了新的错误提示,React.Children. only expected to receive a single react element child
    • 我认为这个错误属于布局代码..你可以检查你的渲染功能。
    • 我更新了我的帖子。它非常直截了当。但我收到了这个错误。
    猜你喜欢
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多