// 喜欢评论 const likeComment = async (_id) => { let res = await Comment.findById(_id) if (res) { // 每次请求,views 都增加一次 res.likes += 1 res = await res.save() } return res }
Address.find({ user: req.user.id }) .then(address => { res.status(200).json({ message: "user addresses", address }); }) .catch(err => { res.status(400).json({ message: "Couldn't get addresses", err }); });
//if false, set isRestricted to false // Change user state (isRestricted) to true User.findOneAndUpdate( { _id: userId }, { $set: { isRestricted: false } }, { new: true, useFindAndModify: false } ).then(user => { res.status(200).json({ message: "User is un Restricted", user }); });
Company.find({}, (err, results) => { if (err) logger.error(err); else if (results) { app.locals.subdomains = []; results.forEach((item) => { app.locals.subdomains.push(item.subdomain.toLowerCase()); }); } });
const shouldFileBeDeleted = async (fileName, beforeLimit) => { try { const file = await StorageFile.findOne({ fileName }) .select('referencesCount updatedAt'); return file === null || (file.referencesCount <= 0 && moment(file.updatedAt) .isBefore(beforeLimit)); } catch (err) { return true; } }
OfferSchema.pre('save', async function onSave(next) { if (this.isNew) { await OfferLocation.createByOffer(this); } next(); });
const deleteUserById = (data) => { if (data.role === 'user' || data.role === 'businessUser') { return false; } return User.findByIdAndRemove(data.params.userId) .catch((err) => { log.error('ERR: ', err); return false; }) .then(() => true); }
async store(req, res) { const tracking = await Tracking.create(req.body) .then(() => { res.status(200).json({ status: 200, message: 'Data insert successfully', type: 'Success' }); console.log("Data updated successfully".green); }).catch((err) => { res.status(500).json({ status: 500, message: 'Error insert data', type: 'Failure' }); console.log(err.message.red); }); }
async index(req, res){ const { page = 1 } = req.query; const products = await Product.paginate({ }, { page: page, limit: 10}); return res.json(products); }
const getAdminUser = data => User.findOne({ username: data.body.name }).then((res) => { if (res == null) { return false; } const pass = bcrypt.compareSync(data.body.password, res.password); if (pass) { return genAuthTokens(res); } return false; })
Category.findByIdAndUpdate(req.params.id, updatedCategory, { new: true, useFindAndModify: false }) .then(category => res.json({ message: "Successfully updated", category })) .catch(err => res.json(err));
User.find({ username: { $nin: ["admin"] } }) .sort({ creationDate: -1 }) .select("-password") .then(users => { res.status(200).json({ users }); }) .catch(err => { res.json(err); });
Category.findByIdAndDelete(req.params.id) .then(() => { return res.status(200).json({ message: `Category was deleted Succefully` }); }) .catch(error => { return res .status(400) .json({ error, message: "Couldn't delete, please try again" }); });
// 喜欢回复 const likeReply = async (_id) => { let res = await Reply.findById(_id) if (res) { // 每次评论,reply 都增加一次 res.likes += 1 await res.save() } }
async index(req, res) { const { page = 1 } = req.query; const products = await Product.paginate({}, {page, limit: 10}); return res.json(products); }