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

lean() function bug in document's field with Buffer type! #7964

Closed
DelavariMohsen opened this issue Jul 6, 2019 · 2 comments
Closed

lean() function bug in document's field with Buffer type! #7964

DelavariMohsen opened this issue Jul 6, 2019 · 2 comments

Comments

@DelavariMohsen
Copy link

DelavariMohsen commented Jul 6, 2019

Hi,
I have this model:

_id: mongoose.Types.ObjectId,
name: { type: String, required: true },
mimetype: String,
content: Buffer,

I return content field with this code in Nest js framework:

res.contentType(f.mimetype);
res.send(f.content);

When I find document with this code:

this.files.findById(imageId);

It's OK.
but when I use .lean() function in findById Or findOne and return image to client, Image is not valid image file!

I use:
MongoDb cloud v 4.0.
mongoose: ^5.5.7
@nestjs/mongoose: ^6.1.2,

thanks.

@vkarpov15 vkarpov15 added this to the 5.6.5 milestone Jul 8, 2019
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Jul 8, 2019
@DelavariMohsen
Copy link
Author

DelavariMohsen commented Jul 9, 2019

repro script is ready in my Github

result with lean():
withlean

@vkarpov15
Copy link
Collaborator

Unfortunately this is expected behavior and there isn't much Mongoose can do about it because you're using lean(). The MongoDB driver stores buffers as MongoDB's Binary type. Mongoose converts MongoDB Binary objects into Buffers based on your schema. But we don't do that if you use lean(), because the whole point of lean() is to avoid schema conversion to minimize performance overhead..

TLDR:

    server.get('/getImageWithLean', async (req, res) => {
        const file = await fileModel.findOne({}).lean();
        if (file == null) {
            res.sendStatus(400).send();
            return;
        }
        res.contentType(file.mimetype);
        // Pull out the underlying buffer from the MongoDB Binary instance
        res.send(file.content.buffer);
    });

@vkarpov15 vkarpov15 removed this from the 5.6.5 milestone Jul 9, 2019
@vkarpov15 vkarpov15 added won't fix and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants