第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在 React 中使用 .map() 時(shí),任何人都可以幫助解決類型錯(cuò)誤問題嗎?

在 React 中使用 .map() 時(shí),任何人都可以幫助解決類型錯(cuò)誤問題嗎?

ITMISS 2022-07-01 16:59:48
有人能幫我解決這個(gè)錯(cuò)誤嗎?我正在嘗試為國家名稱創(chuàng)建一個(gè)下拉選擇器,從以下 API 路徑中提取:https ://api.covid19api.com/countriesTypeError: Country.map is not a functionat fetchCountries (index.js:29)at async fetchAPI (CountryPicker.jsx:13)這是我正在處理的兩個(gè)代碼部分:CountryPicker.jsximport React, { useState, useEffect } from 'react';import { NativeSelect, FormControl } from '@material-ui/core';import styles from './CountryPicker.module.css';import { fetchCountries } from '../../api';const CountryPicker = () => {  const [fetchedCountries, setFetchedCountries] = useState([]);  useEffect(() => {    const fetchAPI = async () => {      setFetchedCountries(await fetchCountries());    };    fetchAPI();  }, [setFetchedCountries]);  console.log(fetchedCountries);  return (    <FormControl className={styles.formControl}>      <NativeSelect >        <option value="">Global</option>      </NativeSelect>    </FormControl>  );};export default CountryPicker;index.jsimport axios from 'axios';const summary = 'https://api.covid19api.com/summary';const countriesURL = 'https://api.covid19api.com/countries';export const fetchData = async () => {    try {        const { data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, }, Date } } = await axios.get(summary);        return {  NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, Date };    } catch (error) {        console.log(error);    }}//TODO//Fetch Daily Data for charts using axiosexport const fetchCountries = async () => {    try {      const { data: [ {Country} ] } = await axios.get(countriesURL);     return Country.map((Country) => Country);    } catch (error) {      console.log(error);    }  };我試圖查找錯(cuò)誤可能發(fā)生的原因,我發(fā)現(xiàn) .map() 不適用于不是數(shù)組的變量,但我不確定當(dāng)前的實(shí)現(xiàn),如何解決這個(gè)問題API 中的數(shù)據(jù)如下所示:[    {"Country":"Micronesia, Federated States of","Slug":"micronesia","ISO2":"FM"},    {"Country":"Bangladesh","Slug":"bangladesh","ISO2":"BD"},    {"Country":"Bouvet Island","Slug":"bouvet-island","ISO2":"BV"},    // ...and so on...]
查看完整描述

2 回答

?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

查看調(diào)用 API 的結(jié)果,是這樣的形式:


[

    {"Country":"Micronesia, Federated States of","Slug":"micronesia","ISO2":"FM"},

    {"Country":"Bangladesh","Slug":"bangladesh","ISO2":"BD"},

    {"Country":"Bouvet Island","Slug":"bouvet-island","ISO2":"BV"},

    // ...and so on...

]

所以Country不是數(shù)組,它是數(shù)組中每個(gè)對象的屬性。


如果您的目標(biāo)是Country從每個(gè)對象中提取屬性,請獲取數(shù)組,然后使用map提取該屬性,也許使用解構(gòu):


const data = await axios.get(countriesURL);

return data.map(({Country}) => Country);


查看完整回答
反對 回復(fù) 2022-07-01
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

重組在這里沒有得到妥善處理。


const { data: [ x ] } = {data: [{"Country":"Micronesia, Federated States of","Slug":"micronesia","ISO2":"FM"},{"Country":"Bangladesh","Slug":"bangladesh","ISO2":"BD"}]}

這里x將{"Country":"Micronesia, Federated States of","Slug":"micronesia","ISO2":"FM"}


下一個(gè):


const {Country} = x


國家將在這里"Micronesia, Federated States of"


and"Micronesia, Federated States of"是一個(gè)字符串,它沒有map函數(shù)。


{ data: [ {Country} ] }訪問所有密鑰的錯(cuò)誤方法也是如此。


const fetchCountries = async () => {

  try {

    const countries = await axios

      .get(countriesURL)

      .then((x) => x.data);

    return countries.map(({ Country }) => Country);

  } catch (error) {

    console.log(error);

    return []

  }

};

const fetchCountries = async () => {

  try {

    const countries = await axios

      .get("https://api.covid19api.com/countries")

      .then((x) => x.data);

    return countries.map(({ Country }) => Country).sort();

  } catch (error) {

    console.log(error);

    return []

  }

};

fetchCountries().then(console.log);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>


獲取其他信息:


// const axios = require("axios"); // for node js


const fetchCountriesInfoParallel = async (countries = []) => {

  const promises = countries.map((country) =>

    axios

      .get(`https://api.covid19api.com/live/country/${country}`)

      .then(({ data }) => data)

  );

  try {

    return await Promise.all(promises);

  } catch (error) {

    console.log(error);

    return [];

  }

};

fetchCountriesInfoParallel(["Afghanistan", "Albania", "Algeria"]).then(

  console.log

);


const fetchCountriesInfoSeries = async (countries = []) => {

  let results = [];

  for (let index = 0; index < countries.length; index++) {

    const country = countries[index];

    const data = await axios

      .get(`https://api.covid19api.com/live/country/${country}`)

      .then(({ data }) => data);

    results.push(data);

  }

  return results;

};

fetchCountriesInfoSeries(["Afghanistan", "Albania", "Algeria"]).then(

  console.log

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>


查看完整回答
反對 回復(fù) 2022-07-01
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號