I have my express code below
What i am doing::
- I am renaming the location of the image i receive on the server
- I am also updating the name of the image to database
- I am doing both the things simoultaniously
- 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