简体   繁体   English

给我的_id加上名字和姓氏

[英]Gendered my _id with firstname and lastname

Hey i will generated my _id with firstname and lastname in mongodb but i have problem with this,i will generated id like this : '_id':'midoxnavas' will midox firstname and navas last name 嘿,我将在mongodb中生成带有名字和姓氏的_id,但是我对此有疑问,我将生成这样的ID:'_id':'midoxnavas'将Midox名字和navas姓氏

var bcrypt = require('bcrypt-nodejs'),
    _ = require('underscore'),
    mongoosePaginate = require('mongoose-paginate');

module.exports = function (mongoose) {
    var Schema = mongoose.Schema;

    // noinspection JSAnnotator
    var UserSchema = new Schema({
        _id='firstName'+'lastName',
        firstName: String,
        lastName: String,
        userName: {type: String, unique: true},
        email: {type: String, unique: true},
        password: String,
        salt: String,
        role : {type: String, enum : ['User','Manager','Admin']},
    });

    // generating a hash
    UserSchema.methods.generateHash = function (password) {
        return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
    };

    // checking if password is valid
    UserSchema.methods.validPassword = function (password) {
        return bcrypt.compareSync(password, this.password);
    };

    UserSchema.plugin(mongoosePaginate);

    module.exports = mongoose.model('User', UserSchema);
}

the erreur is 错误是

 _id='firstName'+'lastName',
        ^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid shorthand property initializer

Have you any solution ? 有什么解决办法吗?

_id - generate automaticly. _id自动生成。

I think you need create virtual field docs 我认为您需要创建virtual字段文档

PersonSchema
    .virtual('name.full')
    .get(function () {
        return this.name.first + ' ' + this.name.last;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM