From 76d77dc9929af688b2948ace942ed58f6a8fc85a Mon Sep 17 00:00:00 2001 From: Tim Adams <zfac125@live.rhul.ac.uk> Date: Fri, 16 Feb 2024 23:05:11 +0000 Subject: [PATCH] cleared the error messages and the next step it to match the user input to the product --- public/search.js | 29 +++++++++++++++++++++++------ server.js | 5 +++-- views/index.hbs | 2 +- views/men.hbs | 2 +- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/public/search.js b/public/search.js index f5e6825..962922e 100644 --- a/public/search.js +++ b/public/search.js @@ -3,23 +3,34 @@ const characterList = document.getElementById('characterList'); const searchBar = document.getElementById('search-input'); -console.log('search running'); -console.log(searchBar); +//console.log('search running'); +//console.log(searchBar); searchBar.addEventListener('keyup', (event) => { console.log(event.target.value); const searchString = event.target.value.toLowerCase(); // Convert to lower case for case-insensitive comparison const filteredProducts = products.filter((product) => { + console.log('Product:', product); return ( - product.name.toLowerCase().includes(searchString) || - product.tag.toLowerCase().includes(searchString) + (product.name?.toLowerCase().includes(searchString) || false) || + (product.tag?.toLowerCase().includes(searchString) || false) ); }); - console.log(filteredProducts); + console.log('Search String:', searchString); +products.forEach((products) => { + console.log('Product Name:', products.name); + console.log('Product Tag:', products.tag); +}); + + + //console.log(filteredProducts); displayCart(filteredProducts); // Pass filtered products to displayCart }); + + + displayCart = (products) => { console.log('Products:', products); if (Array.isArray(products)) { @@ -58,7 +69,13 @@ searchBar.addEventListener('keyup', (event) => { const loadProducts = async () => { try { - const res = await fetch('http://localhost:5000'); + const res = await fetch('http://localhost:5000/products', { + headers:{ + accept: 'application/json', + 'User-agent': 'learning app', + } + }); + if (!res.ok) { throw new Error('Network response was not ok'); } diff --git a/server.js b/server.js index a126142..766f8f4 100644 --- a/server.js +++ b/server.js @@ -10,15 +10,16 @@ app.set('view engine', 'hbs'); app.use('/', require('./routes/pages')); app.use('/products', require('./routes/productsRouteFile')); +/* app.use('http://localhost:5000/products'); */ -const productList = require('./products'); +/* const productList = require('./products'); // Route to get products app.get('/products', (req, res) => { // Send the products as JSON res.setHeader('Content-Type', 'application/json'); res.json(productList); - }); + }); */ app.listen(5000, () => { console.log("Server is listening on port 5000"); diff --git a/views/index.hbs b/views/index.hbs index b179a27..c21e252 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -68,7 +68,7 @@ </header> <main class="cont"> - <div class="search-display"></div> + {{!-- <div class="search-display"></div> --}} <div id="characterList"></div> <div class="posts-container"></div> </main> diff --git a/views/men.hbs b/views/men.hbs index ba94f32..920e510 100644 --- a/views/men.hbs +++ b/views/men.hbs @@ -76,7 +76,7 @@ {{!-- <script src="/product-cards.js"></script> --}} <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.6.7/axios.min.js" integrity="sha512-NQfB/bDaB8kaSXF8E77JjhHG5PM6XVRxvHzkZiwl3ddWCEPBa23T76MuWSwAJdMGJnmQqM0VeY9kFszsrBEFrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> - <script type="module" src="/search.js"></script> + <script src="/search.js"></script> <script src="/shopping-basket.js"></script> </body> -- GitLab