0

Good Day. I'm a beginner. I am working on a sales website. A website that will allow customers to view different manufacturers (such as VW, Audi, etc) and different cars. I have successfully created a website but having a problem with certain functionality. Let's say a user clicks on "VW", after clicking the user will be redirected to a page that will only show "VW" cars. The user goes back to the main page and this time he click on "Audi", he is then redirected to a page which will only display Audi manufactured cars. I have associated the manufacturer with each car. The problem is when the user clicks on any one manufacturer, say "Honda", then he should ONLY see cars made by Honda. Currently I see all cars no matter which manufacturer I click. All the cars stored in database are on every manufacturers page. I need to separate them. I usually use "database.findById(req.params.id)" but this time its a bit different so I'm not able to make sense of it. I using node.js and the code is:

app.get("/cars/BMW", function(req, res) {
    carModel.findById(req.body.manufacturer, function(err, foundModels){
        if(err){
            console.log(err);
        } else {
            console.log("BMW cars found");
            res.render("models/bmw", {bmw:foundModels});
        }
    });
}); 

As you can see, I do not want to use findById. I want to use something like "findByManufacturer". It would go something like this carModel.findByManufacturer(req.body.manufacturer, ......). So when the user clicks on BMW logo, he will be redirect to BMW show page and by using "findByManufacturer" we will only display those cars in the database that are manufactured by BMW. I hope it made sense.

Dayron Gallardo
  • 1,502
  • 2
  • 21
  • 37
Haseeb
  • 87
  • 1
  • 13
  • Wich database engine are you using? Assuming you're using MongoDB with Mongoose, you can use the method find() and specify over which column you want to filter your results. Here's the documentation https://mongoosejs.com/docs/api.html#model_Model.find – Dayron Gallardo Sep 28 '18 at 21:06
  • 1
    Possible duplicate of [Find document with array that contains a specific value](https://stackoverflow.com/questions/18148166/find-document-with-array-that-contains-a-specific-value) – Dayron Gallardo Sep 28 '18 at 21:12

0 Answers0