I want to fetch data from mongodb by using document property similarly like findById() method I can fetch with query but I want to display data on another page
This is my api code for fetching data
const User = require("../models/User");
const Image = require("../models/Image");
const addImage = async (req, res, next) => {
const newImage = new Image({ userId: req.user.id, ...req.body });
try {
const saveImage = await newImage.save();
res.status(200).json("Image uploaded");
} catch (error) {
next(error);
}
};
// GETPRODUCTBYID :-
const getImage = async (req, res) => {
try {
const image = await Image.findById(req.params.id);
res.status(200).json(image);
} catch (error) {
res.status(500).json(error);
}
};
// GET ALL PRODUCTS :-
const getAllImages = async (req, res) => {
const qNew = req.query.new;
const qCategory = req.query.category;
const qBrand = req.query.brand;
try {
let images;
if (qNew) {
images = await Image.find().sort({ createdAt: -1 }).limit(1);
} else if (qCategory) {
images = await Image.find({
categories: { $in: [qCategory] },
});
}
if (qBrand) {
images = await Image.find({ brand: "Honda" });
} else {
images = await Image.find();
}
res.status(200).json(images);
} catch (error) {
res.status(500).json(error);
}
};
// GET IMAGES BY BRAND :-
const getImagesByBrand = async (req, res) => {
const qBrand = req.query.brand;
try {
const images = await Image.find( {brand: qBrand});
res.status(200).json(images);
} catch (error) {
res.status(500).json(error);
}
};
module.exports = Object.freeze({
addImage,
getImage,
getImagesByBrand,
getAllImages,
});
Structure of document in mongo db
- Document
- _id
- brand
I want to fetch data with brand property and show it on new page it is possible?