2015-04-29 8 views
5

Per il mio progetto ho creato un userSchema che ha semplificato è simile alla seguente:mangusta pari al altro valore

var userSchema = new Schema({ 
    _id: String, 
    screenname: {type: String, required: false, default: "equal _id"}, 
}); 

L'utente ha un _id che è una stringa che è anche il suo nome utente. Tutto funziona fino a quando non ho provato ad aggiungere un altro screenname per il campo. Quello che voglio è quando l'utente crea un account, il suo screenname è uguale al valore di _id. Più tardi può regolarlo, ma per impostazione predefinita dovrebbe essere uguale al valore di _id. ho anche provato:

screenname: {type: String, required: false, default: _id}, 

Ma rispetto al corso _id non è definito.

Come devo impostare il valore predefinito su un altro valore?

risposta

7

utilizzare il middleware pre spiegato here

userSchema.pre('save', function (next) { 
    this.screenname = this.get('_id'); // considering _id is input by client 
    next(); 
}); 
Problemi correlati