预保存挂钩完成后,javascript node.js猫鼬更新架构的ID

时间:2019-06-07 14:43:56

标签: javascript node.js mongoose

预保存挂钩完成后,如何使用创建的数据ID更新主模式,例如

express.js

userschema.full_name = fullName;
userschema.first_name = firstName;
userschema.last_name = lastName;
userschema.save(function (err, result){

});

Schema.js

var userSchema = mongoose.Schema({
        full_name       : String,
        other_name      : String,
        last_name       : String,
        email           : String,
        employee_id     : {
            type            : Schema.Types.ObjectId,
            ref             : 'employment-detail'
        }
    });

userSchema.pre('save', function (){
    //obtain values from form field
    var field1 = employmentDate;
    var field2 = responsibility;

    const employmentObject = this.model('employment-detail');   
    employmentObjectins = new employmentObject({
                                'employment_date': formField1,
                                'responsibility' :formField2
                        });

    employmentObjectins.save(function (err){
        if(err){
            console.log(err+' error saving object');
        }
        else{
            console.log('no error in saving object');
        }
    });

    //get employmentObjectins._id into userSchema
});

如何将jobObjectins._id转换为userSchema的employee_id

0 个答案:

没有答案