不能GET / products / 5a22c1bdc2f7e634fc7c7c97

时间:2017-12-09 07:35:12

标签: node.js mongodb mlab

请创建一个nodejs应用程序使用mlab作为我的数据库和邮递员作为我发布api的方法,但现在如果我想查看我的产品,我会收到此错误:

  

无法获取/产品/ 5a22c1bdc2f7e634fc7c7c97   以下是我的代码。

Server.js文件:

const express = require('express');
const morgan = require('morgan');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const ejsmate = require('ejs-mate');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const flash = require('express-flash');
const LocalStrategy = require('passport-local').Strategy;
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
var secret = require('./config/secret');
var User = require('./models/user');
var Category = require('./models/category');
var Universities = require('./models/universities');



var app = express();

mongoose.Promise = global.Promise;
var promise = mongoose.connect(secret.database,{useMongoClient: true}, (err) =>{
 if(err){
console.log(err)
}else{
console.log('connected to new database');
}
})

// MiddleWare

app.use(bodyParser.urlencoded({extended:true}));
app.use(cookieParser());
app.use(session({
resave: true,
saveUninitialized: true,
secret: secret.secretKey,
store: new MongoStore({url: secret.database, autoReconnect: true})
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next)=>{
 res.locals.user = req.user;
next();
})

app.use((req, res, next)=>{
Category.find({}, (err, categories)=>{
 if(err) return next(err)
 res.locals.categories = categories;
next();
})
})

app.set("view engine", "ejs");

app.engine('ejs', ejsmate);
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/public"));

var mainRoutes = require('./routes/main');
var userRoutes = require('./routes/user');
var adminRoutes = require('./routes/admin');
var apiRoutes = require('./api/api');

app.use(mainRoutes, function(req, res, next){
next();
})
app.use(userRoutes, function(req, res, next){
next();
})
app.use(adminRoutes, function(req, res, next){
next();

});
app.use('/api', apiRoutes, function(req, res, next){
next();

});


app.listen(secret.port, function(){
console.log(`app is runing on port. ${secret.port}`);
})


Main.js file   
const express = require('express');
const router = express.Router();
var Product = require("../models/product");

router.get('/', function(req, res){
 res.render("main/home");
})

router.get('/about', function(req, res){
 res.render("main/about");
})

router.get('products/:id', function(req, res, next){
 Product
.find({ category: req.params.id })
.populate('category')
.exec(function(err, products){
 if(err) return next(err)
 res.render("main/category", {products: products})
})
});

router.get("/product/:id", (req, res, next)=>{
Product.findById({_id: req.params.id}, (err, product)=>{
 if(err) return next(err);
 res.render("main/product", {
product: product
})
})
})




module.exports = router;
category.ejs


<% layout('layout') -%>

<div class = "container">
<div class ="row">
<% for (var i = 0; i < products.length; i ++){%>
<div class="col-md-4">
<a href="/product/<%=products[i]._id %>">
<div class="thumbnail">
<img src="<%=products[i].image %>">
<h3><%= products[i].name%></h3>
<p><%=products[i].category.name%></p>
<p><%=products[i].intro%></p>
<p><%=products[i].website%></p>
</div>
</a>
</div>
<%}%>
</div>
</div>

那么请如何查看某个类别的产品,但现在只填充了该类别的一个产品(大学)

1 个答案:

答案 0 :(得分:0)

好的我已经能够修复它忘了添加&#34; /&#34;在router.get产品网址中的产品之前。

谢谢。