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
folder controllers
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
folder Routes
router.get('/search', AnnonceController.getAnnonce)
folder 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 DEPARTEMENT WITH INPUT SELECT
<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> <option value="Aube - Troyes">10 - Aube - Troyes</option> <option value="Aude - Carcassonne">11 - Aude - Carcassonne</option> <option value="12">12 - Aveyron</option> <option value="13">13 - Bouches-du-Rhone</option> <option value="14">14 - Calvados</option> <option value="15">15 - Cantal</option> <option value="16">16 - Charente</option> <option value="17">17 - Charente-Maritime</option> <option value="18">18 - Cher</option> <option value="19">19 - Correze</option> <option value="2A">2A - Corse-du-Sud</option> <option value="2B">2B - Haute-Corse</option> <option value="21">21 - Cote-d'Or</option> <option value="22">22 - Cotes-d'Armor</option> <option value="23">23 - Creuse</option> <option value="24">24 - Dordogne</option> <option value="25">25 - Doubs</option> <option value="26">26 - Drome</option> <option value="27">27 - Eure</option> <option value="28">28 - Eure-et-Loir</option> <option value="29">29 - Finistere</option> <option value="30">30 - Gard</option> <option value="31">31 - Haute-Garonne</option> <option value="32">32 - Gers</option> <option value="33">33 - Gironde</option> <option value="34">34 - Herault</option> <option value="35">35 - Ille-et-Vilaine</option> <option value="36">36 - Indre</option> <option value="37">37 - Indre-et-Loire</option> <option value="38">38 - Isere</option> <option value="39">39 - Jura</option> <option value="40">40 - Landes</option> <option value="41">41 - Loir-et-Cher</option> <option value="42">42 - Loire</option> <option value="43">43 - Haute-Loire</option> <option value="44">44 - Loire-Atlantique</option> <option value="45">45 - Loiret</option> <option value="46">46 - Lot</option> <option value="47">47 - Lot-et-Garonne</option> <option value="48">48 - Lozere</option> <option value="49">49 - Maine-et-Loire</option> <option value="50">50 - Manche</option> <option value="51">51 - Marne</option> <option value="52">52 - Haute-Marne</option> <option value="53">53 - Mayenne</option> <option value="54">54 - Meurthe-et-Moselle</option> <option value="55">55 - Meuse</option> <option value="56">56 - Morbihan</option> <option value="57">57 - Moselle</option> <option value="58">58 - Nievre</option> <option value="59">59 - Nord</option> <option value="60">60 - Oise</option> <option value="61">61 - Orne</option> <option value="62">62 - Pas-de-Calais</option> <option value="63">63 - Puy-de-Dome</option> <option value="64">64 - Pyrenees-Atlantiques</option> <option value="35">65 - Hautes-Pyrenees</option> <option value="66">66 - Pyrenees-Orientales</option> <option value="67">67 - Bas-Rhin</option> <option value="68">68 - Haut-Rhin</option> <option value="69">69 - Rhone</option> <option value="70">70 - Haute-Saone</option> <option value="71">71 - Saone-et-Loire</option> <option value="72">72 - Sarthe</option> <option value="73">73 - Savoie</option> <option value="74">74 - Haute-Savoie</option> <option value="75">75 - Paris</option> <option value="76">76 - Seine-Maritime</option> <option value="77">77 - Seine-et-Marne</option> <option value="78">78 - Yvelines</option> <option value="79">79 - Deux-Sevres</option> <option value="80">80 - Somme</option> <option value="81">81 - Tarn</option> <option value="82">82 - Tarn-et-Garonne</option> <option value="83">83 - Var</option> <option value="84">84 - Vaucluse</option> <option value="85">85 - Vendee</option> <option value="86">86 - Vienne</option> <option value="87">87 - Haute-Vienne</option> <option value="88">88 - Vosges</option> <option value="89">89 - Yonne</option> <option value="90">90 - Territoire de Belfort</option> <option value="91">91 - Essonne</option> <option value="92">92 - Hauts-de-Seine</option> <option value="93">93 - Seine-Saint-Denis</option> <option value="94">94 - Val-de-Marne</option> <option value="95">95 - Val-d'Oise</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 RESULT SEARCH
<% if(docs) {%> <h1> <%= docs.titreBien %> </h1> <h1> <%= docs.departement %> </h1> <%} else {%> <h1>OPPS !!!!</h1> <%} %>
result console.log
null