const ProductPage = () => { const [product, setProduct] = useState<Product>(); const…
- User
const ProductPage = () => { const [product, setProduct] = useState(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ product, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(
/products/find/${id}); setProduct(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { if (product?.color) { const res = await axios.get(http://localhost:5000/api/products); setFilter(res.data); } } catch (err) { console.log(err); } }; getFilter(); }, [product?.color]); return ( {product && filter ? ( <> {product.title} {product?.description} {product.price}PLN Color {filter .filter((item) => item.color[0] !== product.color[0]) .map((item, index) => ( <Link key={index} to={/product/${item._id}}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); }; const ProductPage = () => { const [product, setProduct] = useState(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ product, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(/products/find/${id}); setProduct(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { if (product?.color) { const res = await axios.get(http://localhost:5000/api/products); setFilter(res.data); } } catch (err) { console.log(err); } }; getFilter(); }, [product?.color]); return ( {product && filter ? ( <> {product.title} {product?.description} {product.price}PLN Color {filter .filter((item) => item.color[0] !== product.color[0]) .map((item, index) => ( <Link key={index} to={/product/${item._id}}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); };const ProductPage = () => { const [product, setProduct] = useState(); const [filter, setFilter] = useState<Product[]>(); const [quantity, setQuantity] = useState(1); const location = useLocation(); const id = location.pathname.split("/")[2]; const dispatch = useAppDispatch(); const handleQuantity = (type: string) => { if (quantity > 1 && type === "dec") setQuantity(quantity - 1); if (type === "inc") setQuantity(quantity + 1); }; const handleClick = () => { dispatch(addProduct({ product, quantity })); }; useEffect(() => { const getProducts = async () => { try { const res = await publicRequest.get(/products/find/${id}); setProduct(res.data); } catch (err) { console.log(err); } }; getProducts(); }, [id]); useEffect(() => { const getFilter = async () => { try { if (product?.color) { const res = await axios.get(http://localhost:5000/api/products); setFilter(res.data); } } catch (err) { console.log(err); } }; getFilter(); }, [product?.color]); return ( {product && filter ? ( <> {product.title} {product?.description} {product.price}PLN Color {filter .filter((item) => item.color[0] !== product.color[0]) .map((item, index) => ( <Link key={index} to={/product/${item._id}}> ))} <RemoveIcon onClick={() => handleQuantity("dec")} /> {quantity} <AddIcon onClick={() => handleQuantity("inc")} /> <Button onClick={() => handleClick()}>Add to cart </> ) : null} ); };Argument of type '{ product: Product | undefined; quantity: number; }' is not assignable to parameter of type '{ price: number; quantity: number; products: Product[]; }'. Object literal may only specify known properties, but 'product' does not exist in type '{ price: number; quantity: number; products: Product[]; }'. Did you mean to write 'products'?ts(2345) - Assistant
The error indicates that the argument passed to the
addProductfunction does not match the expected type. TheaddProductfunction expects an object with propertiesprice,quantity, andproductsof typenumber,number, andProduct[], respectively. However, the argument being passed has propertiesproductandquantity.To fix the error, you should create a
Productobject that has thepriceproperty from theproductobject and use it as the argument for theaddProductfunction, like this:const handleClick = () => { const newProduct = { ...product, price: product?.price || 0 }; dispatch(addProduct({ products: [newProduct], quantity })); };This creates a new
Productobject that includes all the properties of the originalproductobject, as well as apriceproperty that defaults to0if theproductobject is undefined. It then passes this newProductobject and thequantityto theaddProductfunction.