/
User.js
68 lines (57 loc) · 1.75 KB
/
User.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const mongoose = require("mongoose")
const bcrypt = require('bcrypt')
const Player = require('./Player')
const userSchema = new mongoose.Schema({
username: String,
password: String,
createdAt: {
type: Date,
immutable: true,
default: () => Date.now(),
},
wins: {
type: Number,
min: 0,
default: 0,
},
opponent: {
type: mongoose.SchemaTypes.ObjectId,
ref: "User",
},
roomId: Number,
week: {
type: Number,
default: 0,
},
team: {
//ST: Player.schema,
ST:{ type: Player.schema, default: new Player()},
RW:{ type: Player.schema, default: new Player()},
LW:{ type: Player.schema, default: new Player()},
CAM:{ type: Player.schema, default: new Player()},
CM:{ type: Player.schema, default: new Player()},
CDM:{ type: Player.schema, default: new Player()},
RCB:{ type: Player.schema, default: new Player()},
LCB:{ type: Player.schema, default: new Player()},
RB:{ type: Player.schema, default: new Player()},
LB:{ type: Player.schema, default: new Player()},
GK:{ type: Player.schema, default: new Player()}
},
});
userSchema.methods.sayHi = function() {
console.log(`Hi. My name is ${this.username}`)
}
// Add a method to the schema to compare passwords
userSchema.methods.comparePassword = async function(candidatePassword) {
try {
return await bcrypt.compare(candidatePassword, this.password);
} catch (error) {
throw new Error(error);
}
};
userSchema.statics.findByName = function(username) {
return this.find({username: new RegExp(username, "i")})
}
// userSchema.query.byName = function(name) {
// }
module.exports = mongoose.model("User", userSchema)