I’m newer to Javascript and the proper handling of event listeners; it would greatly appreciated if you could offer any help. My program is an e-commerce project, and i’m currently trying to receive 2 event listeners ( sizeChosen , qty ) which describe the size of the product selected and the quantity or amount of products.
Some Technologies used: MongoDb , Express React , Redux , Node.js , axios
I had encountered this issue when trying to implement the Size event listener selector option, previously when i had only recorded quantity it worked fine. So far I’ve been able to read around and I’ve been able to get the input printed in the DOM via console.log , but whenever I alter
const sizeChosen = document.getElementById(‘sizeChosen’);
[![const sizeChosen = document.getElementById(‘sizeChosen’);][1]][1]
to
const sizeChosen = document.getElementById(‘sizeChosen’).value;
it then causes a flood of these errors that lose me.
[![after using getElementById().value][2]][2]
How can i clean up these errors and safely use the user’s selected size in the next cart.js screen? Upon examing Redux Devtools, the json object shows that the sizeChosen for some reason isn’t receiving the selected size on singleProduct.js.
[![enter image description here][3]][3]
** singleProduct.js -> shows a product from the mongoDB for the client to purchase and select size & quantity wanted with a [add to cart] button that sends user to cart.js **
[![enter image description here][4]][4]
import React, { useEffect, useState } from "react";
import Header from "./../components/Header";
import Rating from "../components/homeComponents/Rating";
import { Link } from "react-router-dom";
import Message from "./../components/LoadingError/Error";
import { useDispatch, useSelector } from "react-redux";
import {
createProductReview,
listProductDetails,
} from "../Redux/Actions/ProductActions";
import Loading from "../components/LoadingError/Loading";
import { PRODUCT_CREATE_REVIEW_RESET } from "../Redux/Constants/ProductConstants";
import moment from "moment";
import ImageGalleryComponent from '../components/homeComponents/image-gallery.component';
const SingleProduct = ({ history, match }) => {
const [qty, setQty] = useState(1);
const [sizeChosen, setSizeChosen] = useState(1);
const [rating, setRating] = useState(0);
const [comment, setComment] = useState("");
const productId = match.params.id;
const dispatch = useDispatch();
const productDetails = useSelector((state) => state.productDetails);
const { loading, error, product } = productDetails;
const userLogin = useSelector((state) => state.userLogin);
const { userInfo } = userLogin;
const productReviewCreate = useSelector((state) => state.productReviewCreate);
const {
loading: loadingCreateReview,
error: errorCreateReview,
success: successCreateReview,
} = productReviewCreate;
useEffect(() => {
if (successCreateReview) {
alert("Review Submitted");
setRating(0);
setComment("");
dispatch({ type: PRODUCT_CREATE_REVIEW_RESET });
}
dispatch(listProductDetails(productId));
}, [dispatch, productId, successCreateReview]);
const AddToCartHandle = (e) => {
e.preventDefault();
history.push(`/cart/${productId}?qty=${qty}`);
};....
** cart.js -> shows all cart items and sizes chosen **
[![enter image description here][5]][5]
import React, { useEffect } from "react";
import Header from "./../components/Header";
import { Link } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { addToCart, removefromcart } from "./../Redux/Actions/cartActions";
const CartScreen = ({ match, location, history }) => {
window.scrollTo(0, 0);
const dispatch = useDispatch();
const productId = match.params.id;
const qty = location.search ? Number(location.search.split("=")[1]) : 1;
const sizeChosen = document.getElementById('sizeChosen');
const cart = useSelector((state) => state.cart);
const { cartItems } = cart;
console.log(sizeChosen);
const total = cartItems.reduce((a, i) => a + i.qty * i.price, 0).toFixed(2);
useEffect(() => {
if (productId) {
dispatch(addToCart(productId, qty));
}
}, [dispatch, productId, qty]);
const checkOutHandler = () => {
history.push("/login?redirect=shipping");
};
const removeFromCartHandle = (id) => {
dispatch(removefromcart(id));
};
const productDetails = useSelector((state) => state.productDetails);
const { loading, error, product } = productDetails;....
full sections of code can be provided if requested via github.Thanks for reading this far, your a champion and true scholar!
[1]: https://i.stack.imgur.com/ESkgC.png
[2]: https://i.stack.imgur.com/84n6C.png
[3]: https://i.stack.imgur.com/go7Af.png
[4]: https://i.stack.imgur.com/qHvcp.png
[5]: https://i.stack.imgur.com/O9fg2.png