I have stored image in mongodb without using GriFS.To store image i have used :
var PictureSchema = new mongoose.Schema({
id :String,
name :String,
img: { data: Buffer, contentType: String }
});
var Picture = db.model("Picture", PictureSchema);
app.post("/upload", function (req, res, next) {
var input = req.body;
var multiparty=require("multiparty");
var form=new multiparty.Form();
form.parse(req,function(err,fields,files){
var imgE=files.image[0];
var picture = new Picture;
picture.id=fields.email[0];
picture.name=fields.name[0];
picture.img.data = fs.readFileSync(imgE.path);
picture.img.contentType = mime.lookup(imgE.path);
picture.save(function (err, picture) {
if (err) throw err;
});
res.redirect('/view');
});
});
app.get('/view', function(req, res){
Picture.find({}, function(err, docs){
//console.log(docs[0].img);
if(err) res.json(err);
else res.render('index', {pictures: docs});
});
});
Its stroed data.Now i am not able to fetch this image in jade.I am trying:
ul
each picture in pictures
li Name: #{picture.name}
li Email: #{picture.id}
br
div.imagetable
img(src= "http://" + picture.img)
but its giving "Cannot read property 'img' of undefined"Actually picture object in img tag is getting undefined while i am able to access this object for name and email.
Please help.