Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

627
Views
Mongoose find() method not working with query parameters

I'm trying to find a particular document in mongodb using Node Js and mongoose .

But when I'm running following code snippet I'm getting no response from database.

const express = require("express");
const router = express.Router();
const Vehicle = require("../db/models/vehicle");

//api to get single vehicle data
router.get("/vehicle", async (req, res, next) => {
  try {
    const { name } = req.query;
    console.log(name);
    const vehicle = await Vehicle.find({ name: name });
    res.status(200).json(vehicle);
  } catch (error) {
    res.status(500).json({ message: error });
  }
});

[postman image with no response][1]

But when I'm running following code snippet I'm getting response from database.

const express = require("express");
const router = express.Router();
const Vehicle = require("../db/models/vehicle");

//api to get single vehicle data
router.get("/vehicle", async (req, res, next) => {
  try {
  
    const vehicle = await Vehicle.find({ name: "BMW II" });
    res.status(200).json(vehicle);
  } catch (error) {
    res.status(500).json({ message: error });
  }
});

[postman image with response][2] What should I do ? [1]: https://i.stack.imgur.com/7svLJ.png [2]: https://i.stack.imgur.com/ZgxRa.png

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As seen by this You are sending your query as name="BMW II" then in the server your name variable will be set to '"BMW II"' Therefore you will not get the correct results.

//your req will have a field like this
req = {query: { name: '"BMW II"' },...}

when sending query strings you do not enclose in quotes even though it is string. query strings in express are by default identified as strings in the backend.
just send in like name=BMW II and your name variable will be set to 'BMW II'.

req = {query: { name: 'BMW II' },...}

In case you are sending a number as a query string say num=5 Then in the backend you will have parse it to an integer if you are willing to do calculations on it since it is received as string from req.query

req = {query: { num: '5' },...}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!