【发布时间】:2020-10-31 22:47:46
【问题描述】:
我注意到唯一改变的是三个函数中的Product.find({type:''}),所以它们都一样...
我可以做一个功能做同样的工作吗?
routes.js 文件:
mainRouter.get('/showChips',showChips);
mainRouter.get('/showJuices',showJuices);
mainRouter.get('/showSoftDrinks',showSoftDrinks);
controller.js 文件:
const showChips = async (req,res)=>{
const chipsItems = await Product.find({type:'chips'});
console.log(chipsItems);
res.json(chipsItems)
};
const showJuices = async (req,res)=>{
const juicesItems = await Product.find({type:'juices'});
console.log(juicesItems);
res.json(juicesItems)
};
const showSoftDrinks = async (req,res)=>{
const softDrinksItems = await Product.find({type:'Soft Drinks'});
console.log(softDrinksItems);
res.json(softDrinksItems)
};
【问题讨论】:
标签: javascript node.js mongodb mongoose routes