1

Would this work say a new user registers for the site and I want the default role to be "user"?

I want that when a person signs up, the default role is set to "user", and be able to change it later to, let's say "admin".

  firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
dob: {
type: Date,
required: true
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
role: {
type: String,
default: 
}
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • no, it would not. you have to use a proxy to intercept missing property gets. alternaticely, you could use the default operator: `n={a:1,b:2}["c"] || 0;` – dandavis Oct 07 '17 at 21:05

2 Answers2

1

Is this some kind of Schema (Mongoose) if yes then yes it will work.

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94
0
default

Is a keyword in JavaScript, so check out

this stackoverflow question and answer

It basically says you're allowed to use keywords as object property names, since ES5. Also, your property will remain unchanged until you change it somehow, so yes, it serves as a default value for that key.

papiro
  • 2,158
  • 1
  • 20
  • 29