简体   繁体   中英

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

I tried to display products to a screen and what i get is that error,here is my code:

{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>
   )}

And i fetch data from an api using useFocusEffect and useCallBack and axios,when app starts products shows but when i try to save any file in the project without making any changes that error appears and when i save again products show up again, i need help and here is another codes:

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([]);
    };
  },
  [],
)

))


when i console.log before return() the first five arrays are empty and the rest contains repeated data, each array contains six products(objects),and and when i console.log inside a map function all the arrays have repeated data like below(but all),the output of console.log is:

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",
},
]

check type of productsCtg if it is Object;

This error means productsCtg is not a array Try:

console.log(productsCtg)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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