Is there a way to know when a mongoose virtual setter is done 'setting'?
for example:
schema.virtual('file").set(function(fileObj) {
var that = this;
var dst = '/somewhere/else.txt';
fs.rename(fileObj.path, dst, function(err) {
that.set("file.path", dst);
that.markModified('file');
//here i would like to know that the setter finished doing stuff so can potentially save my object
}
});
The problem i face is that when i set the file
property of my mongoose model, i have no way to know when the rename is done, so when i save my object, there is a chance that the setter code hasnt finished running.
I know i can just create a method on the model that sets the file, but is there a way to do it in the setter?
Your options are to either:
fs.renameSync
instead of fs.rename
as virtual setter methods must be synchronous.fs.rename
completes.