undefined 不是一个函数(靠近'....data.map.....')|React native
undefined is not a function (near '....data.map....') |React native
我是 React native 的新手,正在尝试使用 map 函数来解构我从 API 调用中获得的 json 响应,但它以某种方式给了我上述错误。我想使用文本组件显示空气质量和主要污染物。我正在使用 AQICN API.
import React,{ useState , useEffect} from 'react';
import { StyleSheet, Text, View ,ActivityIndicator, ScrollView,FlatList} from 'react-native';
import * as Location from 'expo-location';
export default function HomeScreen({navigation}) {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
//Lat and Long
const [latitude, setLatitude] = useState(null);
const [longitude , setLongitude]= useState(null);
const [data, setData] = useState([]);
const [loader, setLoader]=useState(true);
useEffect(() => {
(
async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission Denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
//Changes
setLatitude(location.coords.latitude);
setLongitude(location.coords.longitude);
const la=latitude;
const lo=longitude;
async function AqicnApiCall() {
let res = await fetch("https://api.waqi.info/feed/geo:"+ latitude +";"+ longitude +"/?token=ac3a71fc80931abd95ede14c2040f0678f578703")
.then((response) => response.json())
.then((json) => setData(json.data))
.catch((error) =>console.log(error))
}
AqicnApiCall();
})();
}, [latitude, longitude]);
//const obj=JSON.stringify(data);
return (
<ScrollView style={styles.container}>
{
data.map((d) =>{
console.log(d);
return(
<Text style={styles.container}>{d.data.aqi}</Text>
)
})
}
</ScrollView>
);
}
const styles= StyleSheet.create({
container: {
padding:20,
marginTop:15,
margin:10,
},
paragraph : {
padding:20,
marginTop:5,
}
});
这是API回复,我需要主要污染物和aqi。
我认为 d.data 中的某些内容未定义或为空。请试试这个。
<ScrollView style={styles.container}>
{
data && data.map((d) =>{
console.log(d);
return(
<Text style={styles.container}>{d?.data?.aqi}</Text>
)
})
}
</ScrollView>
由于数据不是数组,我看到您只想显示 aqi
值,
<ScrollView style={styles.container}>
<Text style={styles.container}>{data?.aqi}</Text>
</ScrollView>
截图上好像只能迭代data.attributions
.
<ScrollView style={styles.container}>
{data.attributions.map(attr => <Text>{attr.something}</Text>}
</ScrollView>
工作应用:Expo Snack
访问 aqi
和 dominentpol
,如下所示:
return (
<ScrollView style={styles.container}>
<Text style={styles.container}>AQI: {data?.aqi}</Text>
<Text style={styles.container}>
Dominant Pollutant: {data?.dominentpol}
</Text>
</ScrollView>
);
}
完整工作代码:
import React, { useState, useEffect } from 'react';
import {
StyleSheet,
Text,
View,
ActivityIndicator,
ScrollView,
FlatList,
} from 'react-native';
import * as Location from 'expo-location';
export default function HomeScreen({ navigation }) {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
//Lat and Long
const [latitude, setLatitude] = useState(null);
const [longitude, setLongitude] = useState(null);
const [data, setData] = useState([]);
const [loader, setLoader] = useState(true);
useEffect(() => {
(async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission Denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
//Changes
setLatitude(location.coords.latitude);
setLongitude(location.coords.longitude);
const la = latitude;
const lo = longitude;
async function AqicnApiCall() {
let res = await fetch(
'https://api.waqi.info/feed/geo:' +
latitude +
';' +
longitude +
'/?token=ac3a71fc80931abd95ede14c2040f0678f578703'
)
.then((response) => response.json())
.then((json) => {
console.log('data: ', json.data);
setData(json.data);
})
.catch((error) => console.log(error));
}
AqicnApiCall();
})();
}, [latitude, longitude]);
//const obj=JSON.stringify(data);
return (
<ScrollView style={styles.container}>
<Text style={styles.container}>AQI: {data?.aqi}</Text>
<Text style={styles.container}>
Dominant Pollutant: {data?.dominentpol}
</Text>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
padding: 20,
marginTop: 15,
margin: 10,
},
paragraph: {
padding: 20,
marginTop: 5,
},
});
我是 React native 的新手,正在尝试使用 map 函数来解构我从 API 调用中获得的 json 响应,但它以某种方式给了我上述错误。我想使用文本组件显示空气质量和主要污染物。我正在使用 AQICN API.
import React,{ useState , useEffect} from 'react';
import { StyleSheet, Text, View ,ActivityIndicator, ScrollView,FlatList} from 'react-native';
import * as Location from 'expo-location';
export default function HomeScreen({navigation}) {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
//Lat and Long
const [latitude, setLatitude] = useState(null);
const [longitude , setLongitude]= useState(null);
const [data, setData] = useState([]);
const [loader, setLoader]=useState(true);
useEffect(() => {
(
async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission Denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
//Changes
setLatitude(location.coords.latitude);
setLongitude(location.coords.longitude);
const la=latitude;
const lo=longitude;
async function AqicnApiCall() {
let res = await fetch("https://api.waqi.info/feed/geo:"+ latitude +";"+ longitude +"/?token=ac3a71fc80931abd95ede14c2040f0678f578703")
.then((response) => response.json())
.then((json) => setData(json.data))
.catch((error) =>console.log(error))
}
AqicnApiCall();
})();
}, [latitude, longitude]);
//const obj=JSON.stringify(data);
return (
<ScrollView style={styles.container}>
{
data.map((d) =>{
console.log(d);
return(
<Text style={styles.container}>{d.data.aqi}</Text>
)
})
}
</ScrollView>
);
}
const styles= StyleSheet.create({
container: {
padding:20,
marginTop:15,
margin:10,
},
paragraph : {
padding:20,
marginTop:5,
}
});
这是API回复,我需要主要污染物和aqi。
我认为 d.data 中的某些内容未定义或为空。请试试这个。
<ScrollView style={styles.container}>
{
data && data.map((d) =>{
console.log(d);
return(
<Text style={styles.container}>{d?.data?.aqi}</Text>
)
})
}
</ScrollView>
由于数据不是数组,我看到您只想显示 aqi
值,
<ScrollView style={styles.container}>
<Text style={styles.container}>{data?.aqi}</Text>
</ScrollView>
截图上好像只能迭代data.attributions
.
<ScrollView style={styles.container}>
{data.attributions.map(attr => <Text>{attr.something}</Text>}
</ScrollView>
工作应用:Expo Snack
访问 aqi
和 dominentpol
,如下所示:
return (
<ScrollView style={styles.container}>
<Text style={styles.container}>AQI: {data?.aqi}</Text>
<Text style={styles.container}>
Dominant Pollutant: {data?.dominentpol}
</Text>
</ScrollView>
);
}
完整工作代码:
import React, { useState, useEffect } from 'react';
import {
StyleSheet,
Text,
View,
ActivityIndicator,
ScrollView,
FlatList,
} from 'react-native';
import * as Location from 'expo-location';
export default function HomeScreen({ navigation }) {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
//Lat and Long
const [latitude, setLatitude] = useState(null);
const [longitude, setLongitude] = useState(null);
const [data, setData] = useState([]);
const [loader, setLoader] = useState(true);
useEffect(() => {
(async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission Denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
//Changes
setLatitude(location.coords.latitude);
setLongitude(location.coords.longitude);
const la = latitude;
const lo = longitude;
async function AqicnApiCall() {
let res = await fetch(
'https://api.waqi.info/feed/geo:' +
latitude +
';' +
longitude +
'/?token=ac3a71fc80931abd95ede14c2040f0678f578703'
)
.then((response) => response.json())
.then((json) => {
console.log('data: ', json.data);
setData(json.data);
})
.catch((error) => console.log(error));
}
AqicnApiCall();
})();
}, [latitude, longitude]);
//const obj=JSON.stringify(data);
return (
<ScrollView style={styles.container}>
<Text style={styles.container}>AQI: {data?.aqi}</Text>
<Text style={styles.container}>
Dominant Pollutant: {data?.dominentpol}
</Text>
</ScrollView>
);
}
const styles = StyleSheet.create({
container: {
padding: 20,
marginTop: 15,
margin: 10,
},
paragraph: {
padding: 20,
marginTop: 5,
},
});