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.sstatic.net/7svLJ.png [2]: https://i.sstatic.net/ZgxRa.png
console.log(name);print? Do you actually get a valid name in your request?