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: 
}
});
1
  • 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; Commented Oct 7, 2017 at 21:05

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it's a Mongoose schema.
glad to help :)
Thanks for accepting the answer :) also please remember in future also mention which technology you are talking about you haven't mention mongoose anywhere which would deviate users from answering the right answers. Just remember.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.