Nodejs蓝鸟诺言在处理图像时失败

//Created a promise for each image size. var promises = sizes.map(function (size) { return new Promise(function (resolve, reject) { var destinationDir = fileUtil.getAbsolutePathOfImage(destinationPath); fileUtil.createDirectoryIfNotExists(destinationDir); destinationDir += size.src; fileUtil.createDirectoryIfNotExists(destinationDir); //Resize the image. //console.log('imagefile : ' + JSON.stringify(imageFile)); //console.log('destinationDir: ' + JSON.stringify(destinationDir)); //Called an imageUtil resize method to perform resize. imageUtil.resize(imageFile, destinationDir, size).then(data => { var fileName = destinationPath + size.src + '/' + data; resolve(imageUtil.createImageData(fileName, size.height, size.width)); }).catch(err => { console.error(err); return reject(err); }); }); }); Promise.all(promises) .then(savedImages => { console.log('saved Images are: ' + JSON.stringify(savedImages)); return res.status(200).json(savedImages); }).catch(err => { console.log('i am here' + JSON.stringify(err.message)); return res.status(400).json(JSON.stringify(err.message)); }); ---------------------Resize method of imageutil--------------- var Promise = require('bluebird'), gm = require('gm'), path = require('path'), fs = require('fs'); Promise.promisifyAll(gm.prototype); module.exports = { resize(imageFile, destinationPath, size){ if (!imageFile || !destinationPath || !size) { return; } return new Promise(function (resolve, reject) { // If we just passed callback directly, errors would be fatal var fileName = fileUtil.getFileName(imageFile); //console.log('sourceFile : ' + JSON.stringify(imageFile)); //console.log('saveDirectory : ' + JSON.stringify(destinationPath)); //console.log('fileName is :' + fileName); //Create a write stream. var writeStream = fs.createWriteStream(destinationPath + '/' + fileName); //console.log('Saving at location: ' + writeStream.path); gm(imageFile) .resize(size.width, size.height, '^') .gravity('Center') .crop(size.width, size.height) .writeAsync(writeStream.path, function (err) { if (err) { var error = 'Error while creating image of resolution : ' + size.width + 'x' + size.height + '.'; console.error(JSON.stringify(error)); return reject(new Error(error)); } }); resolve(fileName); }); } }; 

*似乎一切正常,它创build了四个图像文件已损坏,稍后给我错误,但请求处理成功。 我的image processing控制台输出如下:

 saved Images are: [{"src":"/uploads/300/fhjXFLgqq59F91uFK_2h8GiS.jpg","height":"200","width":"300"},{"src":"/uploads/120/fhjXFLgqq59F91uFK_2h8GiS.jpg","height":"120","width":"120"},{"src":"/uploads/48/fhjXFLgqq59F91uFK_2h8GiS.jpg","height":"48","width":"48"}] 

POST / api / upload / image / 200 51.790 ms – 241“创build分辨率的图像时出错:120×120”。 “创build分辨率图像时出现错误:48×48”。 “创build分辨率的图像时出错:300×200”。*

       

网上收集的解决方案 "Nodejs蓝鸟诺言在处理图像时失败"

既然你已经使用了promisifyAll ,你不需要(也不应该 )使用Promise构造函数。 write Async已经返回一个诺言 – 如果你不传递callback,因为这是蓝鸟能够传递callback本身在正确的位置的要求。

你应该使用

 module.exports = { resize(imageFile, destinationPath, size){ if (!imageFile || !destinationPath || !size) { return Promise.reject(new Error("missing arguments")); } var fileName = fileUtil.getFileName(imageFile); //console.log('sourceFile : ' + JSON.stringify(imageFile)); //console.log('saveDirectory : ' + JSON.stringify(destinationPath)); //console.log('fileName is :' + fileName); //Create a write stream. var writeStream = fs.createWriteStream(destinationPath + '/' + fileName); //console.log('Saving at location: ' + writeStream.path); var promise = gm(imageFile) .resize(size.width, size.height, '^') .gravity('Center') .crop(size.width, size.height) .writeAsync(writeStream.path); return promise.then(function() { return filename; }, function (err) { var error = 'Error while creating image of resolution : ' + size.width + 'x' + size.height + '.'; console.error(error, err); throw new Error(error); }); } }; 

同样的,你不应该在调用调用时使用Promise构造函数antipattern – 它已经返回一个promise:

 var promises = sizes.map(function (size) { var destinationDir = fileUtil.getAbsolutePathOfImage(destinationPath); fileUtil.createDirectoryIfNotExists(destinationDir); destinationDir += size.src; fileUtil.createDirectoryIfNotExists(destinationDir); //Resize the image. //console.log('imagefile : ' + JSON.stringify(imageFile)); //console.log('destinationDir: ' + JSON.stringify(destinationDir)); return imageUtil.resize(imageFile, destinationDir, size) //^^^^^^ .then(data => { var fileName = destinationPath + size.src + '/' + data; return imageUtil.createImageData(fileName, size.height, size.width)); }, err => { console.error(err); throw err; }); });