1 回答
TA贡献1828条经验 获得超6个赞
您可以创建一个工厂服务并在代码中使用它。像这样的东西。
// ShipSevice.js
const getData = async () => { // you can rename the method
try {
const ships = await Ship.find().sort({ shipName: 1 });
const cruiseLines = await Ship.distinct('cruiseLine');
const reviewCount = await Review.countDocuments();
return { ships, cruiseLines, reviewCount }
} catch(err) {
return {}
}
}
module.exports = { getData }
const { getData } = require('relative-path/ShipSevice');
exports.getIndex = async (req, res, next) => {
try {
// 1) Get tour data from Collection
const { ships, cruiseLines, reviewCount } = await getData()
// 2) Build template
// 3) Render the template from the tour data from step 1
res.status(200).render('main', {
title: 'Welcome',
ships,
reviewCount,
cruiseLines,
});
} catch(err) {
res.status(400).json({ message: 'failed to fetch the'})
}
};
添加回答
举报