// 喜欢评论 const likeComment = async (_id) => { let res = await Comment.findById(_id) if (res) { // 每次请求,views 都增加一次 res.likes += 1 res = await res.save() } return res }
// =========== GraphQL setting ========== // app.use('/graphql', async (req, res, done) => { var userId = (req.auth && req.auth.id ) ? req.auth.id : undefined; const user = ( userId ) ? await User.findById(userId): undefined; req.context = { user: user, } done(); });
// 根据id获取文章 const getArticleById = async (_id) => { let res = await Article.findById(_id).populate('tag') if (res) { // 每次请求,views 都增加一次 res.meta.views += 1 res = await res.save() } return res }
const updateUserCountry = data => User.findByIdAndUpdate(data.params.userId, { country: data.body.country }).then(async (user) => { if (user == null) { return false; } const updatedUser = await User.findById(user.id); return updatedUser; })
User.findById(req.user.id, (err, user) => { if (user.username != "admin") { res.status(400).json({ message: "You can't delete, please contact your admin" }); } else { Product.findByIdAndDelete(req.params.id, err => { if (err) { res.status(400).json({ message: "Couldn't delete, try again" }); } else { res.status(200).json({ message: "Deleted Successfully" }); } }); } });
const addFavorite = async (req, res, next) => { try { const offer = await Offer.findById(req.params.offerId); const updatedOffer = await offer.update({ favorites: offer.favorites + 1 }); log.info('entity was updated %s', updatedOffer.id); return res.json({ status: 'OK', message: 'Share count added', updatedOffer }); } catch (e) { return next(e); } }
Product.findById(req.params.id) .populate("category") .populate("seller") .exec(function (err, result) { if (err) { res.status(404).json({ message: "Not Found" }); } else { res.json(result); } });
const getOfferById = data => Offer.findById(data.params.offerId).then((offer) => { if (data.role === 'user' || data.role === 'businessUser') { return false; } if (offer == null) { return false; } return offer.views ? createDateObejct(offer) : offer; })
async show(req, res) { const tracking = await Tracking.findById(req.params.id) .catch((err) => { res.status(500).json({ status: 500, message: 'This id does not exist', type: 'Failure' }); console.log(err.message.red); }); return res.json(tracking); }
async show(req, res){ const product = await Product.findById(req.params.id); return res.json(product); }
// 喜欢文章 const likeArticle = async (_id) => { let res = await Article.findById(_id) if (res) { // 每次请求,views 都增加一次 res.meta.likes += 1 res = await res.save() } return res }
const getBusinessById = data => Business.findById(data.params.businessId).then((business) => { if (data.role === 'user' || data.role === 'businessUser') { return false; } if (business == null) { return false; } return business; })
const updateUserCategories = data => User.findByIdAndUpdate(data.params.userId, { categories: data.body.categories }).then(async (user) => { if (user == null) { return false; } const updatedUser = await User.findById(user.id); return updatedUser; })
// 喜欢回复 const likeReply = async (_id) => { let res = await Reply.findById(_id) if (res) { // 每次评论,reply 都增加一次 res.likes += 1 await res.save() } }