viewController.js
const Book = require('./../models/bookModel')
const APIFeatures = require('./../utils/apiFeatures.js')
exports.getOverview=async(req,res,next)=>{
const features = new APIFeatures(Book.find(),req.query).filter().paginations()
let getItAllProd = await features.getBook;
res.render('overview',{
data:getItAllProd,
title:"trending books"
})
next()
}
exports.getOneBook =async(req,res,next)=>{
const getThatBook = await Book.findOne({slug:req.params.id})
res.render('onebook',{
data:getThatBook
})
next()
}
exports.categories= async(req,res,next)=>{
const getCate = await Book.find({category:req.params.category});
res.render('overview',{
data:getCate,
name:req.params
})
next()
}
exports.paginationOfBook = async(req,res,next)=>{
let pages = req.params.page
pages = pages * 6 || 6;
const limit = 6;
let skip = pages-6;
const getlimitedbook = await Book.find({}).limit(limit).skip(skip);
res.render('overview',{
data:getlimitedbook,
})
next()
}
viewRoutes.js
const express = require('express');
const viewController = require('./../controllers/viewController')
const router = express.Router();
router.get('/overview',viewController.getOverview)
router.get('/overview/:category',viewController.categories)
router.get('/overview/:page',viewController.paginationOfBook)
router.get('/overview/:id',viewController.getOneBook)
module.exports = router;
使用http://localhost:3000/overview/:category,我可以获取所有书籍,而使用http://localhost:3000/overview/:category,我可以按类别获取书籍。但是,如果我尝试加载http://localhost:3000/overview/:page和http://localhost:3000/overview?:id的数据,数据将无法加载。因此,下面我对路由进行了一些更改。
router.get('/overview',viewController.getOverview)
router.get('/overview/:id',viewController.getOneBook)
router.get('/overview/:category',viewController.categories)
router.get('/overview/:page',viewController.paginationOfBook)
现在我可以访问'/overview'和'/overview/:Id'。但不能访问其他人。没有为他们加载数据。您有任何解决方案吗?这样我就可以访问所有路线了。uuu
1条答案
按热度按时间ymdaylpp1#
首先请你把你的问题详细说明一下.但我认为问题出在下面这一行:
将此行编辑为:
并将此路由命名为http://localhost:3000/overview/34234232 ...或者在下面的行中将“id”更改为“slug”。
并将此路由命名为:
并在调用此路由时传递图书的slug属性值。