1

I am trying to change some binary data from my uploaded images to base64 so I can use that to display an image. But the terimal is giving me this error:

TypeError: Cannot read property 'on' of undefined

I don't understand, when I post I also use the .on event and it is working fine. Besides that, I wonder if I am correctly changing the data.

Please take into account that I'm fairly new to node :)

How I save a uploaded image (POST)

// Post to profile page
router.post('/', function(req, res, next) {
   var busboy = new Busboy({ headers: req.headers });
 busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  var conn = mongoose.createConnection('192.168.99.100:32773');
  conn.once('open', function () {
     var gfs = Grid(conn.db);
      var writestream = gfs.createWriteStream({
        filename: filename,
         content_type: mimetype,
         mode: 'w',
         metadata: {
           belongs_to: req.session.passport.user
         }
      });

      file.pipe(writestream);
      writestream.on('close', function(file){
       res.render('profile', {
        user: req.user,
        message: req.flash('uploadMessage', 'Your image has been uploaded successfully!')
       })
      })
  })
 })
 req.pipe(busboy);
});

Here I try to get a image and convert the binary data to base64 (GET)

// GET to index
router.get('/', function(req, res){
 var conn = mongoose.createConnection('192.168.99.100:32773');
 conn.once('open', function () {
    var gfs = Grid(conn.db);
  var readstream = gfs.createReadStream({
   filename: 'kittendj.jpg'
  });
  readstream.pipe();
  readstream.on('open', function(chunk){
   bufs.push(chunk);
  })
  readstream.on('close', function(){
   var bufs = [];
      var fbuf = Buffer.concat(bufs);
      var base64 = (fbuf.toString('base64'));
      res.render('index', {
    isAuthenticated: req.isAuthenticated(),
    user: req.user,
    imageSrc: '<img src="data:image/jpeg;base64,' + base64 + '">'
   })
  })
 })
});

Resources I checked:

Community
  • 1
  • 1
Murderlon
  • 11
  • 1
  • 4
  • use more semi-colons, it might start working. – dandavis Jan 28 '16 at 05:00
  • @dandavis Semicolons are actually optional if I'm not mistaken, because ECMAScript (the standard for Node.js and browser JavaScript implementations) has an automatic semicolon-insertion feature. So I'm not sure if the problem lies there but feel free to prove me wrong :) – Murderlon Jan 28 '16 at 11:10

2 Answers2

2

I am also looking for the solution to read the image from gridfs and i am using grid-fs strem

this is the solution I found, hope it is helpful for you.

// set up the gridfs

import mongoose from 'mongoose';
import Grid from 'gridfs-stream';

const db = mongoose.connection.db;
const mongoDriver = mongoose.mongo;
const gfs = new Grid(db, mongoDriver);

// write the image to mongo

const writeStream = gfs.createWriteStream({
  filename: 'test.png',
  content_type: 'image/png',
});
fs.createReadStream(filePath).pipe(writeStream);

writeStream.on('close', (gfsFile) => {
  // remove the original file
  fs.unlink('test.png');
  // this is the information, and _id is the id 
  console.log(gfsFile);
});

// read the image to mongo

const readstream = gfs.createReadStream({
  _id: id,
});

const bufs = [];
readstream.on('data', function (chunk) {
  bufs.push(chunk);
});
readstream.on('end', function () {
  const fbuf = Buffer.concat(bufs);
  const base64 = fbuf.toString('base64');
  console.log(base64);
});
Thomas Lee
  • 1,001
  • 2
  • 13
  • 31
0

Please check this Github repo i've created to upload images and return back result as base64. https://github.com/houssem-yahiaoui/fileupload-nodejs.

Houssem Yahiaoui
  • 553
  • 6
  • 14