⛧
KPCKevin Powell - Community
Created by on 2/1/2024 in #back-end
Data fetching issues?
First Snippet:
const handleGetAllProducts = async () => {
try {
const response = await fetch('/api/products', {
method: 'GET',
})
if (response.ok) {
const data = await response.json()
console.log('All products:', data)
} else {
console.error('Failed to get all products')
}
} catch (error) {
console.error('Error getting all products:', error)
}
}
const handleGetAllProducts = async () => {
try {
const response = await fetch('/api/products', {
method: 'GET',
})
if (response.ok) {
const data = await response.json()
console.log('All products:', data)
} else {
console.error('Failed to get all products')
}
} catch (error) {
console.error('Error getting all products:', error)
}
}
Second Snippet:
import products from '../../pages/api/products'

<div className={styles.productsNumber}>{products.length}</div>
import products from '../../pages/api/products'

<div className={styles.productsNumber}>{products.length}</div>
I think I left out details because I wanted to sleep. Though the snippets are not equal, but they should show the total number there and there. I was wondering why there is a discrepancy in the total numbers. Seems after a nap I can think of why it works that way. Items should have been loaded asynchronously, and useState should be used to make sure the correct quantity of items is displayed. My bad for being not that informative at night
4 replies