Home > Blockchain >  Mapping throughproperty of mongodb document
Mapping throughproperty of mongodb document

Time:08-16

I want to fetch data from document using nodejs I am using similar method that we use to display product detail page everything is working but map operator gets data with similar names twice how can i prevent that can i use filter instead of map and how....

this is my code for fetching data

  useEffect(() => {
const getimages = async () => {
  try {
    const res = await publicRequest.get("images");
      setFeeds(res.data);
  } catch (error) {
    console.log(error);
  }
};
getimages();
  }, []);

return (
    <>
      {feeds &&
        feeds.map((data) => (                           // Problem is here
          <Link to={`/car-brand/${data?.brand}`}>
            <Flex
              alignItems={"center"}
              justifyContent="center"
              flexDirection={"row"}
              marginTop={"1rem"}
              sx={{
                "@media screen and (max-width: 960px)": {
                  flexDirection: "column",
                },
              }}
            >
              <div>
                <motion.div
                  whileHover={{
                    scale: 1.1,
                    boxShadow:
                      "rgba(60, 64, 67, 0.3) 0px 1px 2px 0px, rgba(60, 64, 67, 0.15) 0px 2px 6px 2px",
                  }}
                  style={CardStyle}
                >
                  <img
                    style={Image}
                    // src={data.thumbnailImg}
                    alt="Card image cap"
                  />
                </motion.div>
              </div>
            </Flex>
          </Link>
        ))}
    </>
  );

What i want looks like below

  1. Document

    • _id = any id
    • brand = xyz
  2. Document

    • _id = any id
    • brand = hfgj
  3. Document

    • _id = any id
    • brand = xyz

As u can see above list document 1 and 3 have unique id but brand property is same now what i want is to get only 1 and 2 document bcz 1 and 3 has same value in brand property can i achieve this and how.....?

This is my 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 (res) => {
  try {
    const 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,
});

What i am doing is i am using GET IMAGES BY BRAND method to fetch data which gives me all three document now i want only 1 and 2 is it possible?

CodePudding user response:

Yes you can use filter operator , as follows -

feeds.filter((feed, i, arr) => !arr.slice(0, i).find(el => el.brand === feed.brand))
  • Related