关于async的series问题
发布于 2个月前 作者 dabianzhixing 163 次浏览 来自 问答

部分源码如下 async.series([function(callback){ var writeStream = gfs.createWriteStream(option); var write = fs.createWriteStream(‘C:\Users\lqy\Desktop\gm.jpg’); var write2 = fs.createWriteStream(‘C:\Users\lqy\Desktop\err.txt’) stdout.pipe(writeStream); stdout.pipe(write); stderr.pipe(write2); console.log(‘jjjj’); callback(null,’1’) }, function(callback){ fs.unlink(path, function(err,result) { if(err) console.log(err); else console.log(‘kkkk’); res.end(‘3’); }); callback(null,’1’); } ], function(err,result) { if(err) console.log(err); else console.log(result); }); }

不知道为什么,每次执行的时候,总是会先执行fs.unlink,而不是第一个函数的流操作,问什么呢?

3 回复

这样发代码看起来费力啊

新手伤不起啊,我把完整的代码传一下,大家看看到底怎么回事 router.post('/’, function(req, res) {

res.header('Content-Type', 'text/plain');
console.log(req.files);
var path = req.files.file.path; //获取用户上传过来的文件的当前路径
console.log(path);
var gfs =new Grid(conn.db,mongoose.mongo);
var options={
    filename: req.files.file.name +'---3',
    mode: 'w',
    chunkSize: 256,
    content_type: req.files.file.extension,
    root: 'my_collection',
    metadata: {
    }
};
var writestream = gfs.createWriteStream(options);
fs.createReadStream(path).pipe(writestream);

if (req.files.file.mimetype.split('/')[0] != 'image') {
    fs.unlink(path, function() {
        res.end('2');
    });
} else {
    imageMagick(path)
        .resize(150, 150, '!')
        .stream(function (err, stdout, stderr) {
            if(err) console.log(err);
            var option = {
                filename: req.files.file.name,
                mode: 'w',
                chunkSize: 256,
                content_type: req.files.file.extension,
                root: 'thumbnail',
                metadata: {}
            };

            console.log(option);
            async.series([function(callback){
                    console.log('jjjj');
                    var writeStream = gfs.createWriteStream(option);
                    var write = fs.createWriteStream('C:\\Users\\lqy\\Desktop\\gm.jpg');
                    var write2 = fs.createWriteStream('C:\\Users\\lqy\\Desktop\\err.txt')
                    console.log('jjjj');
                    stdout.pipe(writeStream);
                    stdout.pipe(write);
                    console.log('jjjj');
                    stderr.pipe(write2);
                    console.log('jjjj');
                    callback(null,'1')
                },
                function(callback){
                    console.log('yyyy');
                    fs.unlink(path, function(err,result) {
                        console.log('yyyy');
                        if(err)
                            console.log(err);
                        else
                            console.log('kkkk');
                        res.end('3');
                    });
                    callback(null,'1');
                }
                ],
                function(err,result)
                {
                    if(err)
                        console.log(err);
                    else
                        console.log(result);
                });
        }
    )
}

});

问题主要是两个,一个是总是生成不了缩略图,只会产生一个空的图文件,报的错误是 Permission denied @ error/blob.c/OpenBlob/2692. 另一个问题是,最前面的往数据库输入的流,也就是options和下面那部分,也是无法输入,只要我删除fs.unlink那么,缩略图正常,但是数据库输入仍然没有,为什么呢?
回到顶部