Hi everyone, I have encountered a small problem in your code, take a look at this excerpt from the rm function:
adapter.rm = (fd, cb) => {
const errorHandler = (err, client) => {
if (client) client.close();
if (cb) cb(err);
}
client(options.uri, options.mongoOptions, (err, client) => {
if (err) {
errorHandler(err, client);
}
bucket(client.db(), options.bucketOptions).delete(fd, (err) => errorHandler(err, client));
if (cb) cb();
});
}
I need a little attention on these two lines:
bucket(client.db(), options.bucketOptions).delete(fd, (err) => errorHandler(err, client));
if (cb) cb();
- The second line is always executed if cb exists.
- The first line performs the deletion, but if no file is found with the given fd, errorHandler is called, this function in turn calls cb passing an err, if cb exists.
The problem encountered is that cb is called twice, if cb exists and no file is found with fd passed to the function, thus meeting the two rules mentioned above. So for a cb like this:
fileAdapter.rm(req.param('fd'), (err) => {
if (err) {
res.send(err.message);
}
else {
res.send('File deleted');
}
});
res.send is called twice, one containing the err and again without err generating the following error:
[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Forgiveness for English, text translated by Google Translate.
Hi everyone, I have encountered a small problem in your code, take a look at this excerpt from the rm function:
I need a little attention on these two lines:
The problem encountered is that cb is called twice, if cb exists and no file is found with fd passed to the function, thus meeting the two rules mentioned above. So for a cb like this:
res.send is called twice, one containing the err and again without err generating the following error:
Forgiveness for English, text translated by Google Translate.