Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write() callback not working, and connection not closing #45

Open
vrmerlin opened this issue Sep 14, 2019 · 5 comments
Open

write() callback not working, and connection not closing #45

vrmerlin opened this issue Sep 14, 2019 · 5 comments
Assignees
Labels

Comments

@vrmerlin
Copy link

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

@lykmapipo
Copy link
Owner

Hello @vrmerlin Please check

@vrmerlin
Copy link
Author

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
Copy link
Author

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.

@johnlohit
Copy link

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

@lykmapipo lykmapipo self-assigned this Sep 23, 2019
@lykmapipo lykmapipo added the bug label Sep 23, 2019
@akanshgulati
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants