我正在尝试上传图像并使用下面的代码更新数据库集合中的图像的URL.
Controller.prototype.handle = function (req,res,next) { var id = req.params.id,controller = req.params.controller,optionalController; optionalController = _.clone(controller); // handle optional controller if (controller === 'newboat') { controller = 'boat'; } else if (controller === 'newcrew') { controller = 'crew'; } var files = req.files.files || {},monthYear = new Date(),monthYear = [monthYear.getFullYear(),monthYear.getMonth()],filename = files[0].originalFilename,path = files[0].path,extension = filename.match(EXPRESSION_EXTENSION)[1],destFolder = [ROOT,monthYear[0],'/',monthYear[1] ].join(''),destination = [ROOT,monthYear[1],id,'.',extension ].join(''); // log info //console.log(id,controller,filename,extension,destination,path); var opts = JSON.stringify({id: id,photo: ['/uploads/',extension ].join('') }); async.series([ uploadImage(path,destFolder,destination),ProcessUpload(optionalController,opts,res) ]); }; // write file var uploadImage = function(path,destination) { // The land of callbacks ------>>>>>> inception within inception mkdirp(destFolder,777,function (err) { fs.readFile(path,function (err,data) { fs.writeFile(destination,data,function (err) { if (err) { console.log('image not uploaded..'); return false; }else { return true; } }); }); }); }; var ProcessUpload = function(optionalController,res ) { opts = JSON.parse(opts); if (optionalController === 'boat') { updateBoatDatabase(opts); } }; /** * Update boat photo to database */ var updateBoatDatabase = function (opts,callback) { // save to database return dbServices.dbBoat.updatePhoto(opts,callback); };
/** * Update image */ updatePhoto: function (opts,callback) { var id = opts.id; // find boat by id this.modelClass.findById(id,model) { if (err || !model) return callback(err); // set update value model.photo = opts.photo; // save model.save(callback); }); },
我在上传图像时遇到以下错误,但图像上传到服务器上,并且其URL也在数据库集合上更新.我假设它与我的回调有关,但我不确定我哪里出错了.任何关于如何调试这个的提示将不胜感激.
TypeError: task is not a function at /home/work/bluewatertracks/node_modules/async/lib/async.js:689:13 at iterate (/home/work/bluewatertracks/node_modules/async/lib/async.js:265:13) at async.forEachOfSeries.async.eachOfSeries (/home/work/bluewatertracks/node_modules/async/lib/async.js:284:9) at _parallel (/home/work/bluewatertracks/node_modules/async/lib/async.js:688:9) at Object.async.series (/home/work/bluewatertracks/node_modules/async/lib/async.js:710:9) at Controller.handle (/home/work/bluewatertracks/server/routers/photoUploader.js:56:9) at Layer.handle [as handle_request] (/home/work/bluewatertracks/node_modules/express/lib/router/layer.js:82:5) at next (/home/work/bluewatertracks/node_modules/express/lib/router/route.js:110:13) at Form.<anonymous> (/home/work/bluewatertracks/node_modules/connect-multiparty/index.js:101:9) at emitNone (events.js:72:20) at Form.emit (events.js:166:7) at maybeClose (/home/work/bluewatertracks/node_modules/connect-multiparty/node_modules/multiparty/index.js:557:10) at endFlush (/home/work/bluewatertracks/node_modules/connect-multiparty/node_modules/multiparty/index.js:552:3) at WriteStream.<anonymous> (/home/work/bluewatertracks/node_modules/connect-multiparty/node_modules/multiparty/index.js:617:5) at emitNone (events.js:72:20) at WriteStream.emit (events.js:166:7)
解决方法
您还可以通过将null对象传递给bind()来简化此操作:
async.series( [ uploadImage.bind( null,path,destination ),ProcessUpload.bind( null,optionalController,res ) ] );