Skip to content

Commit f4a8abb

Browse files
committed
update some methods in user model to async await
1 parent 8dc7b64 commit f4a8abb

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

server/models/user.js

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -199,37 +199,38 @@ userSchema.methods.findMatchingKey = function findMatchingKey(
199199
* @callback [cb] - Optional error-first callback that passes User document
200200
* @return {Promise<Object>} - Returns Promise fulfilled by User document
201201
*/
202-
userSchema.statics.findByEmail = function findByEmail(email, cb) {
203-
let query;
204-
if (Array.isArray(email)) {
205-
query = {
206-
email: { $in: email }
207-
};
208-
} else {
209-
query = {
210-
email
211-
};
212-
}
202+
userSchema.statics.findByEmail = async function findByEmail(email) {
203+
const user = this;
204+
const query = Array.isArray(email) ? { email: { $in: email } } : { email };
205+
213206
// Email addresses should be case-insensitive unique
214207
// In MongoDB, you must use collation in order to do a case-insensitive query
215-
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
208+
const userFoundByEmail = await user
209+
.findOne(query)
210+
.collation({ locale: 'en', strength: 2 })
211+
.exec();
212+
return userFoundByEmail;
216213
};
217214

218215
/**
219216
*
220217
* Queries User collection by emails and returns all Users that match.
221218
*
222219
* @param {string[]} emails - Array of email strings
223-
* @callback [cb] - Optional error-first callback that passes User document
224220
* @return {Promise<Object>} - Returns Promise fulfilled by User document
225221
*/
226-
userSchema.statics.findAllByEmails = function findAllByEmails(emails, cb) {
222+
userSchema.statics.findAllByEmails = async function findAllByEmails(emails) {
223+
const user = this;
227224
const query = {
228225
email: { $in: emails }
229226
};
230227
// Email addresses should be case-insensitive unique
231228
// In MongoDB, you must use collation in order to do a case-insensitive query
232-
return this.find(query).collation({ locale: 'en', strength: 2 }).exec(cb);
229+
const usersFoundByEmails = await user
230+
.find(query)
231+
.collation({ locale: 'en', strength: 2 })
232+
.exec();
233+
return usersFoundByEmails;
233234
};
234235

235236
/**
@@ -239,29 +240,31 @@ userSchema.statics.findAllByEmails = function findAllByEmails(emails, cb) {
239240
* @param {string} username - Username string
240241
* @param {Object} [options] - Optional options
241242
* @param {boolean} options.caseInsensitive - Does a caseInsensitive query, defaults to false
242-
* @callback [cb] - Optional error-first callback that passes User document
243243
* @return {Promise<Object>} - Returns Promise fulfilled by User document
244244
*/
245-
userSchema.statics.findByUsername = function findByUsername(
245+
userSchema.statics.findByUsername = async function findByUsername(
246246
username,
247-
options,
248-
cb
247+
options
249248
) {
249+
const user = this;
250250
const query = {
251251
username
252252
};
253+
253254
if (
254-
(arguments.length === 3 && options.caseInsensitive) ||
255-
(arguments.length === 2 &&
256-
typeof options === 'object' &&
257-
options.caseInsensitive)
255+
arguments.length === 2 &&
256+
typeof options === 'object' &&
257+
options.caseInsensitive
258258
) {
259-
return this.findOne(query)
259+
const foundUser = await user
260+
.findOne(query)
260261
.collation({ locale: 'en', strength: 2 })
261-
.exec(cb);
262+
.exec();
263+
return foundUser;
262264
}
263-
const callback = typeof options === 'function' ? options : cb;
264-
return this.findOne(query, callback);
265+
266+
const userFoundByUsername = await user.findOne(query).exec();
267+
return userFoundByUsername;
265268
};
266269

267270
/**
@@ -276,37 +279,39 @@ userSchema.statics.findByUsername = function findByUsername(
276279
* default query for username or email, defaults
277280
* to false
278281
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
279-
* @callback [cb] - Optional error-first callback that passes User document
280282
* @return {Promise<Object>} - Returns Promise fulfilled by User document
281283
*/
282-
userSchema.statics.findByEmailOrUsername = function findByEmailOrUsername(
284+
userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
283285
value,
284-
options,
285-
cb
286+
options
286287
) {
287-
let isEmail;
288-
if (options && options.valueType) {
289-
isEmail = options.valueType === 'email';
290-
} else {
291-
isEmail = value.indexOf('@') > -1;
292-
}
288+
const user = this;
289+
const isEmail =
290+
options && options.valueType
291+
? options.valueType === 'email'
292+
: value.indexOf('@') > -1;
293+
293294
// do the case insensitive stuff
294295
if (
295-
(arguments.length === 3 && options.caseInsensitive) ||
296-
(arguments.length === 2 &&
297-
typeof options === 'object' &&
298-
options.caseInsensitive)
296+
arguments.length === 2 &&
297+
typeof options === 'object' &&
298+
options.caseInsensitive
299299
) {
300300
const query = isEmail ? { email: value } : { username: value };
301-
return this.findOne(query)
301+
const foundUser = await user
302+
.findOne(query)
302303
.collation({ locale: 'en', strength: 2 })
303-
.exec(cb);
304+
.exec();
305+
306+
return foundUser;
304307
}
305-
const callback = typeof options === 'function' ? options : cb;
308+
306309
if (isEmail) {
307-
return this.findByEmail(value, callback);
310+
const userFoundByEmail = await user.findByEmail(value);
311+
return userFoundByEmail;
308312
}
309-
return this.findByUsername(value, callback);
313+
const userFoundByUsername = await user.findByUsername(value);
314+
return userFoundByUsername;
310315
};
311316

312317
/**

0 commit comments

Comments
 (0)