0

The code which i have written takes id from url and then gives me the output. api?category=4556 here 4556 is the id. I have one Products controller having

  • product_name
  • price
  • category_id:i am passing category id manually from category generated.

and in category controller i have category_name.

here i want to get in this way api?category=games is there any way to do so.

exports.getProducts = function(req, res) {
  // Use the Prod model to find all products
 /* 
  Prod.find(function(err, prods) {
    if (err)
      res.send(err);
    res.json(prods);
    next();
  });
*/

  var cat=req.query.category;
  var condition = {};

  if (cat) {
    condition.cat_id = cat;
  }

  Prod.find(condition).then(function(cat) {
    res.json({cat});
  })
  .catch((err) => {
    console.log('error', err);
    res.status(500).send();
  });
};

This code checks for the products having cat_id similar to the id provided by url as ?category=4556 i want to check it as ?category=games , if there is some way please help it would be great pleasure.thanks in advance

3 Answers 3

1

I found one answer and this really works using populate

var cat=req.query.category;
  var condition = {};

  if (cat) {
    condition.cat_id = cat;
  }

  Prod.find(condition).populate('cat_id').then(function(cat) {
    res.json({cat});
  })
  .catch((err) => {
    console.log('error', err);
    res.status(500).send();
  });
};

and did a bit change in Schema

var ProductSchema=new mongoose.Schema({
    name:String,
    category:String,
    price:Number,
    cat_id:{
        type : mongoose.Schema.Types.ObjectId,
        ref : 'Cat'
    }
});

// Export the Mongoose model
module.exports=mongoose.model('Prod',ProductSchema);

Sign up to request clarification or add additional context in comments.

Comments

0

You will have to retrieve first the category ID from the name that was supplied in the query.

When no category with the supplied name is found, you may want to return an empty products dataset. I also make the assumption that category name are unique.

Example :

exports.getProducts = function(req, res) {
  var categoryName = req.query.category;

  var condition = {};
  if (categoryName) {
    condition.name = categoryName;
  }

  Category.find(condition).then(function(categories) {
    if(categories.length === 0) return {}
    return Product.find({ category_id: categories[0].id })
  }).then(function(products) {
    res.json(products);
  }).catch((err) => {
    console.log('error', err);
    res.status(500).send();
  });
};

2 Comments

this gives me empty array
You have to adapt a bit the code to make it work. For example cat_id, I changed it to make it more explicit to category_id. But populating in mongoose as you found seems to me a better solution.
0

If you want to find correspond to your category name rather than your id then you have to do only one change. Fetch category name from request query (as currently you are fetching id from request query). Rest your code will be same.

I am thinking that your category name is unique also.

exports.getProducts = function(req, res) {
// Use the Prod model to find all products
/* 
Prod.find(function(err, prods) {
  if (err)
   res.send(err);
  res.json(prods);
  next();
});
*/

var cat=req.query.categoryName;
var condition = {};

if (cat) {
 condition.cat_Name = cat;
}

Prod.find(condition).then(function(cat) {
 res.json({cat});
})
.catch((err) => {
 console.log('error', err);
 res.status(500).send();
});
};

Hope it will work for you. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.