简体   繁体   中英

I am trying to use search product from DB, in nodejs but the product is not getting?

I am trying to search product from DB, in nodejs but the product is not getting??? here is my code: this is my search container in my header

user-header.hbs

<div class="search-container">
        <form class="form-inline" action="/search" method="get">
          <input class="search-input" id="search-box" type=" search" placeholder="Search..." name="searchTerm"
            aria-label="Search" />
        </form>
      </div>

user.js

// GET: search box
router.get("/search",async (req, res) => {
   try {
      let searchTerm = req.body.searchTerm;
      let user = req.session.user;
      let product= await  productHelpers.getAllProducts({Name:{ $search: searchTerm,$options: "i", $diacriticSensitive: true }});
      res.render('user/search',{product,user});
      console.log('searchTerm>',searchTerm)
      console.log('products>',product)
    } catch (error) {
      res.status(500).send({message: error.message || "Error Occured" });
      res.redirect("/"); 
    } 
 });

user-helpers.js from user-helpers geting all products fron collections

  getAllProducts:()=>{ 

        return new Promise(async(resolve,reject)=>{
             let products=await db.get().collection(collection.PRODUCT_COLLECTION).find().toArray()
             resolve(products)
        })
     },

search.hbs Search result not getting

<h1 class="pb-4">Search Results</h1>

<div class="row row-cols-2 row-cols-lg-5 g-2 g-lg-3">
  <a href="/" class="col text-center category__link">
    <div class="category__img category__img--large shadow">
      <img src="/product-images/{{product._id}}.jpg" alt="{{product.Name}}" loading="lazy">
    </div>
    <div class="pt-1">
      {{products.Name}} </div>
  </a>
</div> 

my Assumption in user.js something wrong with the code!

Let me guess!

  • The name of the field you want to search is "Name"?
  • If so? then have you created text index in the collection on the "Name" field?
  • And shouldn't the syntax be like {$text: { $search: searchTerm }}

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