const getAdminUser = data => User.findOne({ username: data.body.name }) .then((res) => { if (res == null) { return false; } return genAuthTokens(res); })
// 1- first we get the shipper to get his area Shipper.findOne({ user: userId }, (err, shipper) => { // 2- we get all the order's that has the same area as shipper Order.find() .sort({ orderDate: -1 }) .populate("address") .populate({ path: "products.product", model: "Product" }) .exec((err, orders) => { let areaOrders = orders.filter(order => order.address.state == shipper.area); res.json({ areaOrders }); }); });
// this will find a single record based on id and return it. single(options) { return this.model.findOne({_id: options.id}) .exec() .then(record => { return record; }) .catch(error => { return error; }); }
Wishlist.findOne({ user: userId }) .populate("items.product") .exec((err, wishlist) => { if (err) { res.status(400).json({ message: "Couldn't find wish List", err }); } else { if (!wishlist) { res.status(400).json({ message: "no wish List" }); } else { res.status(200).json({ message: "wish List", wishlist }); } } });
async store (req, res) { const { email } = req.body; if (await User.findOne({ email })) { return res.status(400).json({ error: 'User already exists' }); } const user = await User.create(req.body); return res.json(user); }
const itDoesNotAlreadyExistsAtFavourites = async (req, res, next) => { try { const doc = await FavouriteOffer.findOne({ userId: req.$user.id, offerId: req.$offer.id }); if (doc) { throw new APIError('This offer is already in favourites', httpStatus.BAD_REQUEST, true); } return next(); } catch (err) { return next(err); } }
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; } }
// 获取用户信息 const getUserInfo = async (username) => { return await User.findOne({ username }, 'name username slogan gravatar role') }
const initAdminUserCreation = () => User.findOne({ username: 'admin' }) .then((doc) => { if (doc === null) { return createAdmin(); } return null; }) .then((doc) => { if (doc) { log.info('admin created'); return null; } log.info('admin user exist'); })
const getLoginAdmin = data => User.findOne({ username: data.username }).then((res) => { if (res == null) { return false; } const pass = data.password === res.password; if (pass) { return genAuthTokens(res); } return false; })
// 登录接口 const findOne = async (username) => { return await User.findOne({ username }).exec() }
async store (req, res) { const { email, password } = req.body; const user = await User.findOne({ email }); if (!user) { return res.status(400).json({ error: 'User not found' }); } if (!(await user.compareHash(password))) { return res.status(400).json({ error: 'Invalid password' }); } return res.json({ user, token: User.generateToken(user) }); }
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; })
async store (req, res) { const { email } = req.body if (await User.findOne({ email })) { return res.status(400).json({ error: 'User already exists' }) } const user = await User.create(req.body) return res.json(user) }
async store (req, res) { const { email, password } = req.body const user = await User.findOne({ email }) if (!user) { return res.status(400).json({ error: 'User nor found' }) } if (!(await user.compareHash(password))) { return res.status(400).json({ error: 'Invalid password' }) } return res.json({ user, token: User.generateToken(user) }) }