如果 data.length > 0 响应则无法不同

Unable to different if data.length > 0 on response

我正在做一个 React 本机项目,我在其中查询一个搜索词组。 但是,在我的条件语句中,无论数组中是否包含数据,我都会得到 Not empty。我做错了什么?

这是第一个数组,如果它找到匹配的服务器端查询:

[
    {
        "document": {
            "name": "projects/warrenty-7707e/databases/(default)/documents/users/HBC/warrentycard/d4XgBWIywU6K2fZ7ix0j",
            "fields": {
                "comment": {
                    "stringValue": ""
                },
                "model": {
                    "stringValue": ""
                },
                "createDate": {
                    "timestampValue": "2019-06-27T11:04:47.031Z"
                },
                "product": {
                    "stringValue": ""
                },
                "exp_date": {
                    "stringValue": ""
                },
                "plate": {
                    "stringValue": ""
                },
                "email": {
                    "stringValue": ""
                },
                "name": {
                    "stringValue": ""
                }
            }
        }
    }
]

如果不匹配,这里是数组:

[
    {}
]

无论哪种方式,在以下条件中,我只会得到 Not Empty 返回给我。有什么想法吗?

if (this.state.serverData.length > 0) {
    return (

        <View>
<Text>Not empty</Text>
</View>
    )

} else {
<View>
<Text>empty</Text>
</View>
} ```

问题是,即使没有搜索结果,你从服务器得到的结果数组也不为空。

您可以检查数据的长度是否不为 0(您正在这样做)以及第一个对象是否不为空。

    if (this.state.serverData.length > 0 && Object.keys(this.state.serverData[0]).length !== 0 ) {
        return (<View>
                <Text>Not empty</Text>
            </View>
        )
    }
    return (<View>
    <Text>empty</Text>
    </View>);