1

I have my express code below

What i am doing::

  1. I am renaming the location of the image i receive on the server
  2. I am also updating the name of the image to database
  3. I am doing both the things simoultaniously
  4. I am using async for this

My Problem::

  • So there are two call backs in async in the program
  • Only my first callback functionality works and the other one is not working
  • Though i have defined both the tasks perfectly fine still, handling them at a time, i am not able to resolve it

app.js

var express=require('express');
var fs=require('fs');
var http=require('http');
var crypto=require('crypto');
var mysql=require('mysql');
var async=require('async');

var app=express();

var connection=mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'ImagePostingDB'
});

connection.connect();
app.set('port',process.env.PORT||7002); 
app.use('/Details',express.static(__dirname+'/public/images')); 
app.use(express.bodyParser());

app.post('/Details/',function(req,res,next)
{
    var file_name=req.files.key.originalFilename;
    console.log(file_name);
    async.series( [
       // Get the first table contents
       function ( callback ) 
       {
          crypto.randomBytes(8, function(ex, buf) {

                var array     = req.files.key.originalFilename.split('.');
                var type      = array[array.length - 1];
                var name      = buf.toString('hex') + '.' + type;

                fs.rename(req.files.key.path, './public/images/' + name, function(e) {


                        if (e) {
                                res.send(500, e.message);
                                } else 
                                {
                                    res.send("I got the message - This i confirm");
                                }

                });
            });
       },
       // Updating the database
       function ( callback ) 
       {

            connection.query('INSERT INTO ImagePostingTABLE (Image_Name) VALUES (?)', [file_name], function (err, rows, fields)
            {
                console.log('Connection result error ' + err);
            });
       }
    ] );
});

app.get('/Details/',function(req,res){
        res.send("Image displayed");
});

http.createServer(app).listen(app.get('port'),function(){
        console.log('Express server listening on port'+app.get('port'));

});

How can i resolve this

Hope i am clear

3 Answers 3

1

You need to call the callback that is passed to the task functions of async.series Here is the code

app.post('/Details/',function(req,res,next) {
  var file_name=req.files.key.originalFilename;

  console.log(file_name);
  async.series( [
    // Get the first table contents
    function ( callback ) {
      crypto.randomBytes(8, function(ex, buf) {

        var array = req.files.key.originalFilename.split('.');
        var type  = array[array.length - 1];
        var name  = buf.toString('hex') + '.' + type;

        fs.rename(req.files.key.path, './public/images/' + name, function(e) {

          if (e) {
            res.send(500, e.message);
          } else {
            res.send("I got the message - This i confirm");
          }

          return callback(null);
        });
      });
    },
    // Updating the database
    function ( callback ) {
      connection.query('INSERT INTO ImagePostingTABLE (Image_Name) VALUES (?)', [file_name], function (err, rows, fields) {
        console.log('Connection result error ' + err);
        return callback(null);
      });
    }
  ]);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Please read the documentation on the Async's series method (click here)

In order to move to the next function in the series, the callback needs to be invoked

async.series([
  function(callback){
    crypto.randomBytes(8, function(ex, buf) {
      var array     = req.files.key.originalFilename.split('.');
      var type      = array[array.length - 1];
      var name      = buf.toString('hex') + '.' + type;
      fs.rename(req.files.key.path, './public/images/' + name, function(e) {
        if (e) {
          res.send(500, e.message);
        } else {
          res.send("I got the message - This i confirm");
        }
        callback(null); // Pass whatever you think is appropriate
      });
    });
  },
  function(callback){
      connection.query('INSERT INTO ImagePostingTABLE (Image_Name) VALUES (?)', [file_name], function (err, rows, fields) {
          console.log('Connection result error ' + err);
          callback(err, rows)
        });
  }
]);

Comments

1

With the help of C Blanchard & Bulkan ..... i got solution for this


Here is the complete solution :: This may help someone looking something similar

var express=require('express');

    var fs=require('fs');
    var http=require('http');
    var crypto=require('crypto');
    var mysql=require('mysql');
    var async=require('async');

    var app=express();

    var connection=mysql.createConnection({
        host: 'localhost',
        user: 'root',
        database: 'ImagePostingDB'
    });

    connection.connect();
    app.set('port',process.env.PORT||7002); 
    app.use('/Details',express.static(__dirname+'/public/images')); 
    app.use(express.bodyParser());

    app.post('/Details/',function(req,res,next) {
      var file_name=req.files.key.originalFilename;

      console.log(file_name);
      async.series( [
        // Get the first table contents
        function ( callback ) {
          crypto.randomBytes(8, function(ex, buf) {

            var array = req.files.key.originalFilename.split('.');
            var type  = array[array.length - 1];
            var name  = buf.toString('hex') + '.' + type;

            fs.rename(req.files.key.path, './public/images/' + name, function(e) {

              if (e) {
                res.send(500, e.message);
              } else {
                res.send("I got the message - This i confirm");
              }

              return callback(null);
            });
          });
        },
        // Updating the database
        function ( callback ) {
          connection.query('INSERT INTO ImagePostingTABLE (Image_Name) VALUES (?)', [file_name], function (err, rows, fields) {
            console.log('Connection result error ' + err);
            return callback(null);
          });
        }
      ]);
    });

    app.get('/Details/',function(req,res){
            res.send("Image displayed");
    });

    http.createServer(app).listen(app.get('port'),function(){
            console.log('Express server listening on port'+app.get('port'));

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.