Skip to content

Commit

Permalink
Fix issue with Model.findById() no longer accepts a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Romāns Djatlovs committed May 30, 2023
1 parent d0c5d6c commit 3158235
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 71 deletions.
50 changes: 26 additions & 24 deletions es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,27 @@ function createFileSchema(bucket) {
*/
FileSchema.methods.unlink = function unlink(done) {
// obtain file details
return this.constructor.findById(
// eslint-disable-next-line no-underscore-dangle
this._id,
function afterFindFile(error, file) {
// back-off error
if (error) {
return done(error);
}
// remove file from gridfs
this.constructor
.findById(
// eslint-disable-next-line no-underscore-dangle
this._id
)
.then((file) => {
return bucket.deleteFile(
// eslint-disable-next-line no-underscore-dangle
file._id,
function afterDeleteFile($error /* , id */) {
done($error, file);
}
);
}
);
})
.catch((error) => {
done(error);
});

/*
*/
};

/* statics */
Expand Down Expand Up @@ -299,19 +302,18 @@ function createFileSchema(bucket) {
* });
*/
FileSchema.statics.unlink = function unlink(_id, done) {
return this.findById(_id, function afterFindById(error, file) {
// back-off error
if (error) {
return done(error);
}

if (!file) {
return done(new Error('not found'));
}

// remove file from gridfs
return file.unlink(done);
});
this.findById(_id)
.then((file) => {
if (!file) {
return done(new Error('not found'));
}

// remove file from gridfs
return file.unlink(done);
})
.catch((error) => {
done(error);
});
};

// return grifs schema
Expand Down
50 changes: 26 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,27 @@ function createFileSchema(bucket) {
*/
FileSchema.methods.unlink = function unlink(done) {
// obtain file details
return this.constructor.findById(
// eslint-disable-next-line no-underscore-dangle
this._id,
function afterFindFile(error, file) {
// back-off error
if (error) {
return done(error);
}
// remove file from gridfs
this.constructor
.findById(
// eslint-disable-next-line no-underscore-dangle
this._id
)
.then((file) => {
return bucket.deleteFile(
// eslint-disable-next-line no-underscore-dangle
file._id,
function afterDeleteFile($error /* , id */) {
done($error, file);
}
);
}
);
})
.catch((error) => {
done(error);
});

/*
*/
};

/* statics */
Expand Down Expand Up @@ -300,19 +303,18 @@ function createFileSchema(bucket) {
* });
*/
FileSchema.statics.unlink = function unlink(_id, done) {
return this.findById(_id, function afterFindById(error, file) {
// back-off error
if (error) {
return done(error);
}

if (!file) {
return done(new Error('not found'));
}

// remove file from gridfs
return file.unlink(done);
});
this.findById(_id)
.then((file) => {
if (!file) {
return done(new Error('not found'));
}

// remove file from gridfs
return file.unlink(done);
})
.catch((error) => {
done(error);
});
};

// return grifs schema
Expand Down
48 changes: 25 additions & 23 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,27 @@ function createFileSchema(bucket) {
*/
FileSchema.methods.unlink = function unlink(done) {
// obtain file details
return this.constructor.findById(
// eslint-disable-next-line no-underscore-dangle
this._id,
function afterFindFile(error, file) {
// back-off error
if (error) {
return done(error);
}
// remove file from gridfs
this.constructor
.findById(
// eslint-disable-next-line no-underscore-dangle
this._id
)
.then((file) => {
return bucket.deleteFile(
// eslint-disable-next-line no-underscore-dangle
file._id,
function afterDeleteFile($error /* , id */) {
done($error, file);
}
);
}
);
})
.catch((error) => {
done(error);
});

/*
*/
};

/* statics */
Expand Down Expand Up @@ -296,19 +299,18 @@ function createFileSchema(bucket) {
* });
*/
FileSchema.statics.unlink = function unlink(_id, done) {
return this.findById(_id, function afterFindById(error, file) {
// back-off error
if (error) {
return done(error);
}

if (!file) {
return done(new Error('not found'));
}
this.findById(_id)
.then((file) => {
if (!file) {
return done(new Error('not found'));
}

// remove file from gridfs
return file.unlink(done);
});
// remove file from gridfs
return file.unlink(done);
})
.catch((error) => {
done(error);
});
};

// return grifs schema
Expand Down

0 comments on commit 3158235

Please sign in to comment.