lykmapipo / mongoose-gridfs

mongoose gridfs on top of new gridfs api
MIT License
91 stars 39 forks source link

unlink() does not check errors and empty results #7

Closed cbratschi closed 7 years ago

cbratschi commented 7 years ago

The following code in schema.js should be changed from:

GridFSSchema.statics.unlinkById =
        GridFSSchema.statics.unlink = function(id, done) {
            this.findById(id).exec(function(error, foundInstance) {
                foundInstance.unlink(done);
            });
        };

To:

GridFSSchema.statics.unlinkById =
        GridFSSchema.statics.unlink = function(id, done) {
            this.findById(id).exec((error, foundInstance) => {
                if (error) {
                     return done(error);
                }

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

                foundInstance.unlink(done);
            });
        };

Currently if the file does not exist in GridFS the whole app will crash.

lykmapipo commented 7 years ago

@cbratschi I will appreciate PR for this.

Thanks.

lykmapipo commented 7 years ago

@cbratschi Fixed and PR merged. Thanks