Search in a nodejs using mongoose on input select

when I select a department and I launch the search the page is displayed but I get null in my console.log as it is the first time that I am doing a project with a search bar on nodejs mongodb.
Thank you for your help

my Code =
Models

 const mongoose = require('mongoose')

const AnnonceSchema = mongoose.Schema({
    titreBien:{
        type: String,
        require: true
    },
    description:{
        type: String,
        require: true
    },

    email:{
        type: String,
        require: true
    },

    address:{
        type: String,
        require: true
    },
    
    departement:{
        type: String,
        require: true
    },

    Categories:{
        type: String,
        require: true
    },

    EtatBien:{
        type: String,
        require: true
    },
  
    description:{
        type: String,
        require: true
    },

    image1Upload:{
        type: String,
        require: true
    },

    image2Upload:{
        type: String,
        require: true
    },


    date:{
        type: Date,
        default: Date.now
    },
})

const annonces = mongoose.model('annonces', AnnonceSchema)


module.exports = annonces

Router

router.get('/search', AnnonceController.getAnnonce)

Controllers

const Annonce = require('../models/Annonce')


 const getAnnonce = (req, res) =>{

  Annonce.findOne({departement: req.query.departement}, (err, docs) =>{
    if(err) throw err
    console.log(docs);
    res.render('annonce', {docs: docs})
  })
     
 }


module.exports={ getAnnonce: getAnnonce}

PAGE Search with select EJS

<form action="/search" method="GET" class="form-inline">
        <label  class="m-1" for="departement">
         <span class="homeSelect btn btn-info" style=" font-weight: bold ">Entrer votre département pour bénéficier des biens offerts par votre voisin</span> 
        </label>
        <div class="input-group w-100">
          <select class="form-select" aria-label="Default select example" name="departement" id="departements">
            <option value="Ain">01 - Ain</option>
            <option value="Aisne">02 - Aisne</option>
            <option value="Allier">03 - Allier</option>
            <option value="Alpes-de-Haute-Provence">04 - Alpes-de-Haute-Provence</option>
            <option value="Hautes-Alpe">05 - Hautes-Alpes</option>
            <option value="Alpes-Maritimes">06 - Alpes-Maritimes</option>
            <option value="Ardeche">07 - Ardeche</option>
            <option value="Ardennes">08 - Ardennes</option>
            <option value="Ariege">09 - Ariege</option>
            </select>
          <div class="input-group-btn">
            <button class="btn btn-info float-right"><i class='fas fa-search' style='color:rgb(230, 222, 222)'></i></button>
          </div>
        </div>
      </form>

PAGE RESULTAT SEARCH

  <% if(docs)   {%>
    <h1> <%= docs.titreBien %> </h1>
    <h1> <%= docs.departement %> </h1>
   <%} else  {%>
    <h1>OPPS !!!!</h1>
    <%}   %>

result console.log

null

I don’t see anything at first glance that is wrong.

Are you sure those documents exist in your database? Or maybe it’s a casing issue?

Also worth console.log-ing the req.query itself. Are you sure the data is making it through properly?

1 Like

Ah yes I’m stupid, you’re right, in fact the code is correct, I forgot to submit an ad in the database.
Thank you

1 Like

Haha I wouldn’t say “stupid”, happens to the best of us!

Glad it was an easy fix :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.