1

After the pre save hook is completed how can I update the main schema with the id of the data that was created e.g.

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
});

how can I get employmentObjectins._id into employee_id of the userSchema

S Dra
  • 147
  • 9
  • Possible duplicate of [How do I get the objectID after I save an object in Mongoose?](https://stackoverflow.com/questions/6854431/how-do-i-get-the-objectid-after-i-save-an-object-in-mongoose) – Molda Jun 07 '19 at 14:53
  • No it is not, this one is from inside the schema through a hook. The returned value would go out to where the save operation would take place.. They are different – S Dra Jun 07 '19 at 15:01

0 Answers0