DEV Community

Ankan Saha
Ankan Saha

Posted on

Get image data from mongodb using mongoose and send to client side using nodejs and javascript

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var imageSchema = new Schema({
name: String,
data: Buffer,
contentType: String
});

var Image = mongoose.model('Image', imageSchema);

function getImage(name) {
Image.findOne({ name: name }, function (err, image) {
if (err) throw err;

res.setHeader('Content-Type', image.contentType);
res.send(image.data);
});
}

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay