I have the following query
myModel.findOneAndUpdate({_id: ID},{$push: {myField: myData}},{$new: true})
But pre findOneAndUpdate not work and error occur in its integration with schema:
let options = {
diffOnly: true,
metadata: [
{key: 'user', value: function(origin, d){
let createdAt = new Date(d.createdAt).getTime();
let updatedAt = new Date(d.updatedAt).getTime;
if (createdAt < updatedAt || !operation) {
operation = 'u';
}
return {
id: operation === 'i' ? d.createdBy.id : d.updatedBy.id,
firstName: operation === 'i' ? d.createdBy.firstName : d.updatedBy.firstName,
lastName: operation === 'i' ? d.createdBy.lastName : d.updatedBy.lastName
}
}}
]
};
schema.plugin(mongooseHistory, options);
Error is in d.createdBy, because if I console d, it shows following data:
{ updatedAt: 2019-01-30T08:52:43.770Z, __v: undefined }