I'm writing a web application primarily to serve as a shopping cart. The landing/homepage of the app reflects products that are currently available for sale. What I'd like to do is route to each product using the product ID. I've defined a product model (adminProductModel) as follows:
'use strict';
var mongoose = require('mongoose');
var productModel = function () {
//Define a super simple schema for our products.
var productSchema = mongoose.Schema({
name: String,
price: Number,
productImg: String,
description: String
});
return mongoose.model('Product', productSchema);
};
module.exports = new productModel();
I'm able to post, and get, and delete products using the above model via an admin controller. Works great! I've then gone ahead and created an items controller, model and template, which are defined as below, with the intention to route from the homepage(index.dust) to an item's page using the item/product's id.
The item controller:
'use strict';
var ItemModel = require('../../models/adminProductModel');
var db = require ('../../lib/database');
module.exports = function (router) {
router.get('/index/:id', function (req, res) {
db.ItemModel.findById({_id: req.params._id}, function (err, prod){
if(err){
console.log('FindById filter error:', err)
}
var model = {product: prod}
res.render('item/index', model);
});
});
};
The item model:
'use strict';
module.exports = function ItemModel() {
return {
name: 'item'
};
};
The relevant code on the homepage (index) is: The template (using dust as rendering engine):
{>"layouts/master" /}
{<title}
Greatness!
{/title}
{<body}
{?products}
{#products}
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<a href = "/index/{.id}" class = "thumbnail"><img src="img/photo.png" alt="photo" width ="260" height = "180"/></a>
<center>
<h5>{.name}</h5>
<p>${.price}</p>
<a href="/index/{.id}" class="btn btn-success">Buy</a>
</center>
</div>
</div>
{/products}
{/products}
{/body}
When the anchor tag is clicked, it routes to correct ID, from the index page, but throws the following file not found error "URL /index/542237117b5f3e72136d70c5 did not resolve to a route".
What am I doing wrong here? I know I have to query the database for the products by its unique objectId. Am I implementing this wrong? If so, why does it resolve to a file not found error or at least render the correct markup?
Thanks a ton in advance. I've spend a few days thinking about this and I've exhausted all my approaches to solve this. I've included the database for completeness:
use strict';
var mongoose = require('mongoose');
var db = function () {
return {
config: function (conf) {
mongoose.connect('mongodb://' + conf.host + '/' + conf.database);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('db connection open');
});
}
};
};
module.exports = db();
Would it perhaps be wise to define my itemModel with a separate schema? This seems redundant and more error prone. I'm going to read further into this and home for some feedback from here. Thanks again.
Hi your are getting id value from params but it is :id not :_id
you are doing like this
console.log("id value : "+req.params._id)
db.ItemModel.findById({_id: req.params._id}, function (err, prod){
it should be like this
console.log("id value : "+req.params.id)
db.ItemModel.findById({_id: req.params.id}, function (err, prod){