繁体   English   中英

TypeError: undefined is not a function (near '...productsCtg.map...') 反应原生

[英]TypeError: undefined is not a function (near '...productsCtg.map...') react native

我试图将产品显示到屏幕上,但我得到的是那个错误,这是我的代码:

{productsCtg.length > 0 ? (
   <View style={styles.listContainer}>
       {productsCtg.map((item) => {
         console.log(item);
           return(
               <ProductList
                   navigation={props.navigation}
                   key={item.id}
                   item={item}
               />
           );
       })}
   </View>
   ) : (
       <View style={[styles.center, { height: height / 2}]}>
           <Text style={{fontFamily: 'nunito_semi_bold'}}>No products found</Text>
       </View>
   )}

我使用 useFocusEffect 和 useCallBack 和 axios 从 api 获取数据,当应用程序启动产品时显示,但是当我尝试在项目中保存任何文件而不进行任何更改时出现错误,当我再次保存时产品再次出现,我需要帮助这是另一个代码:

const [products, setProducts] = useState([]);
const [productsFiltered, setProductsFiltered] = useState([]);
const [focus, setFocus] = useState();
const [categories, setCategories] = useState([]);
const [productsCtg, setProductsCtg] = useState([]);
const [active, setActive] = useState();
const [initialState, setInitialState] = useState([]);
const [loading, setLoading] = useState(true)

useFocusEffect((
useCallback(
  () => {
    setFocus(false);
    setActive(-1);
    
    // Products
    axios
      .get(`${baseURL}products`)
      .then((res) => {
        setProducts(res.data);
        setProductsFiltered(res.data);
        setProductsCtg(res.data);
        setInitialState(res.data);
        setLoading(false);
        //console.log(res.data)
      })
      .catch((error) => {
        console.log('Api call error')
      })

    //Categories
    axios
      .get(`${baseURL}categories`)
      .then((res) => {
        setCategories(res.data);
        //console.log(res.data);
      })
      .catch((error) => {
        console.log('Api call error')
      })

    return () => {
      setProducts([]);
      setProductsFiltered([]);
      setFocus();
      setCategories([]);
      setActive();
      setInitialState();
      // setProductsCtg([]);
    };
  },
  [],
)

))


当我在 return() 之前的 console.log 前五个数组为空,其余包含重复数据时,每个数组包含六个产品(对象),并且当我在 map 函数中使用 console.log 时,所有数组都有重复数据,如下所示(但全部),console.log 的输出是:

Array []
Array []
Array []
Array []
Array []
Array [
Object {
"brand": "Arusha",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg- 
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg- 
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg- 
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg- 
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya 
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg- 
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu 
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg- 
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]
Array [
Object {
"brand": "Arusha",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg- 
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg- 
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg- 
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg- 
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya 
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg- 
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu 
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg- 
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]
Array [
Object {
"brand": "Arusha",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 45,
"dateCreated": "2022-06-04T06:32:06.300Z",
"description": "Mtama mzuri sana kutoka arusha",
"id": "629afc66e400878b161e437b",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/21fe5b14-3eee-4218-b554-9a3d17bd801b.jpg- 
1654333522641.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Mtama",
"numReviews": 0,
"price": 42000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dodoma",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 27,
"dateCreated": "2022-06-04T08:59:36.678Z",
"description": "Good quality maize in Dodoma,place your order now.",
"id": "629b1ef89e9b9401aba2ba54",
"image": "http://2fa0-197-250-230- 
109.eu.ngrok.io/public/uploads/fffe96ff-4558-42db-a6b4-7b5b289995be.jpg- 
1654333175940.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 76000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Mbeya",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 16,
"dateCreated": "2022-06-05T10:04:19.040Z",
"description": "Mchele mzuri kutoka mbeya,weka oda yako sasa",
"id": "629c7fa3342fe22635019648",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/8c107f75-31e0-4a6c-9835-27a8f6ada063.jpg- 
1654423458901.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Rice",
"numReviews": 0,
"price": 80000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Tanga",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:39:52.196Z",
"description": "Quality beans available,place your order anytime",
"id": "629c87f8499c526723a96979",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/291ee0f1-369e-45a1-8b43-38756c0d866b.jpg- 
1654425591386.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Beans",
"numReviews": 0,
"price": 85000,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Kilimanjaro",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 12,
"dateCreated": "2022-06-05T10:43:04.778Z",
"description": "Karibu upate ndizi kubwa na zenye ubora wa hali ya 
juu,weka oda yako sasa",
"id": "629c88b8499c526723a9698c",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/4e10f2a7-9ee0-4735-bd8b-75b1af2d48e4.jpg- 
1654425784444.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Ndizi",
"numReviews": 0,
"price": 31500,
"rating": 0,
"richDescription": "undefined",
},
Object {
"brand": "Dar es salaam",
"category": Object {
  "__v": 0,
  "_id": "62925a7db9e8b1361367c733",
  "color": null,
  "icon": null,
  "name": "food crops",
},
"countInStock": 133,
"dateCreated": "2022-06-05T10:47:48.391Z",
"description": "Tunauza mahindi kwa jumla,weka oda yako mapema,karibu 
sana",
"id": "629c89d4499c526723a969a5",
"image": "http://1063-197-250-224- 
205.eu.ngrok.io/public/uploads/6895e399-6254-4c46-a757-2d10a0b188b2.jpg- 
1654426067682.jpeg",
"images": Array [],
"isFeatured": false,
"name": "Maize",
"numReviews": 0,
"price": 73500,
"rating": 0,
"richDescription": "undefined",
},
]

检查 productsCtg 的类型是否为 Object;

此错误表示 productsCtg 不是数组尝试:

console.log(productsCtg)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM