asynchronousfunction不起作用

下面我有我的快递代码

我在做什么 ::

  1. 我正在重命名服务器上收到的图像的位置
  2. 我也正在更新图像的名称到数据库
  3. 我正在同时做这两件事情
  4. 我为此使用asynchronous

我的问题 ::

  • 所以程序中有两个asynchronouscallback
  • 只有我的第一个callback函数工作,另一个不工作
  • 虽然我已经把这两个任务都定义得很好,但是一次处理它们,我无法解决它

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')); }); 

我该如何解决这个问题

希望我清楚

您需要调用传递给async.series的任务函数的callback函数以下是代码

 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); }); } ]); }); 

请阅读Async系列方法的文档( 点击这里 )

为了移动到系列中的下一个function,需要调用callback

 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) }); } ]); 

在C Blanchard & Bulkan的帮助下…..我得到了这个解决scheme


这是完整的解决scheme :: 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')); });