mongoose-gridfs
mongoose-gridfs copied to clipboard
write() callback not working, and connection not closing
I seem to be able to successfully write a file, but have no callback triggered to prove I did it. My code looks roughly like this:
mongoose.connect('mongodb://localhost:27017/niss', { useNewUrlParser: true, useUnifiedTopology: true });
. . .
// use default bucket
const Attachment = createModel({
modelName: 'files',
connection: mongoose.connection
});
const path = 'C:/foo';
const theFile = 'OFFICIAL/LETTERS/foo.pdf';
const readStream = createReadStream(path + theFile);
const options = ({ filename: theFile, contentType: 'application/pdf' });
await Attachment.write(options, readStream, (error, file) => {
if (error) console.log(error);
console.log("Successfully wrote file");
});
Am I not passing in the connection correctly, to the createModel()?
Thanks, John
Hello @vrmerlin Please check
I'm sorry, i see the page you pointed me at, but i don't understand what you are inferring. What's wrong with my above code sample?
if I remove the "connection: mongoose.connection" , so it looks more like your test code, it still creates the file in the mongo database, but still doesn't trigger the callback or. close the connection.
Open up the schema.js file in the source code and try changing line 94 from: this.constructor.findById(created._id, done); to: bucket.findById(created._id, done);
Worked for me
@vrmerlin I think you have used await
in a wrong way here as when the write process is called it will call callback
method and not a promise getting resolved.
You can wrap this login inside a promise to make this work.