A
A
Alexey Yarkov2016-08-13 13:17:49
Mongoose
Alexey Yarkov, 2016-08-13 13:17:49

How to access this model?

Question in the comments to the code

var UserSchema = new Schema({
  // Email пользователя
  email: {
    type: String,
    required: true,
    unique: true
  },
  ........................................
  // API KEY
  apikey: {
    type: String,
    required: true,
    // Как получить доступ к полю email из функции default?
    default: function(){
      return SHA512(this.email).toString().to(50); // this.email не возвращает Email пользователя. Он у всех одинаковый получается.
    }
  },
  ........................................

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2016-08-13
@yarkov

I can suggest using a hook.

UserSchema.pre('save', function() {
  if (!this.apikey) { // проверяем что ещё не задано
    this.apikey = SHA512(this.email).toString().to(50);
  }
});

In this case, default for apikey should just be removed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question