如何使用 fetch 函数将 Api 的响应分配给数组
How to assign response from Api to an array using fetch function
我想在 Fruits 数组中分配响应 我使用 fetch 从 Api 获取数据...但是当 console.log 时我得到空数组..我从 Api 但无法将其分配给水果
我这样做:.then(data => Fruits);
let Fruits = []
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"dfdfdffd"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits);
}, []);
您没有将 data
分配给任何东西。尝试:
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits = data);
此外,您应该使用状态来存储信息。
const [fruits, setFruits] = useState();
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
使用状态
const [fruits, setFruits] = useState([]);
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data);
}, []);
也许使用状态?
const [fruits, setFruits] = useState([])
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
}, []);
console.log(fruits);
我想在 Fruits 数组中分配响应 我使用 fetch 从 Api 获取数据...但是当 console.log 时我得到空数组..我从 Api 但无法将其分配给水果
我这样做:.then(data => Fruits);
let Fruits = []
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"dfdfdffd"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits);
}, []);
您没有将 data
分配给任何东西。尝试:
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits = data);
此外,您应该使用状态来存储信息。
const [fruits, setFruits] = useState();
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
使用状态
const [fruits, setFruits] = useState([]);
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data);
}, []);
也许使用状态?
const [fruits, setFruits] = useState([])
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d.com/audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
}, []);
console.log(fruits);