mongoose-gridfs icon indicating copy to clipboard operation
mongoose-gridfs copied to clipboard

write() callback not working, and connection not closing

Open vrmerlin opened this issue 5 years ago • 5 comments

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

vrmerlin avatar Sep 14 '19 02:09 vrmerlin

Hello @vrmerlin Please check

lykmapipo avatar Sep 16 '19 15:09 lykmapipo

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?

vrmerlin avatar Sep 16 '19 17:09 vrmerlin

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.

vrmerlin avatar Sep 16 '19 23:09 vrmerlin

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

johnlohit avatar Sep 23 '19 06:09 johnlohit

@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.

akanshgulati avatar Sep 27 '20 13:09 akanshgulati