将流写入gridfs-stream

时间:2015-04-28 05:11:41

标签: node.js mongodb

一直在努力让网格(mongodb)在节点中工作,而且我要以完全错误的方式处理它,或者网格系统并不是那么稳定。尝试了三种不同的解决方案,现在决定我将尝试使用gridfs-stream(如果没有人知道更好的解决方案。)

我认为我几乎已经开始工作了。但它只是挂在文件上(我认为)

    var mongoose = require("mongoose");
    var fs = require("fs");
    var mongo = require('mongodb');
    var Grid = require('gridfs-stream');


    mongoose.connect('mongodb://localhost:27017/BabySounds');


    function loadFile(dataCollection, cuID, file){
        console.log("Will create id " + cuID + " in the collection " + dataCollection + " and store " + file);
        var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
        var writeStream = gfs.createWriteStream({
            mode: 'w',
            _id: cuID
        });
        fs.createReadStream(file).pipe(writeStream);
        writeStream.on('close', function (filen) {
            console.log('Written file ' + filen.name);
        });



    }
loadFile( 'fsSound', cuSoundID, srcSound); 

但是,如果你运行上面的代码,它就会创建文件,但是(据我所知)永远不会得到关闭语句writeStream.on('close',

=======更新=======

更新了代码以添加尽可能多的测试。

    var mongoose = require("mongoose");
var fs = require("fs");
var mongo = require('mongodb');
var Grid = require('gridfs-stream');


mongoose.connect('mongodb://localhost:27017/BabySounds');
var db = mongoose.connection;


function loadFile(dataCollection, cuID, file){
    console.log("Will create id " + cuID + " in the collection " + dataCollection + " and store " + file);
    var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
    db.on('error', function(err){
        console.log('Got the following mongoose error: '+ err);
    });
    db.once('connected', function (condata) {
        console.log('The bloody database is open!');
        var writeStream = gfs.createWriteStream({
            mode: 'w',
            _id: cuID
        });
        console.log('And now lets write the thing');
        readStream = fs.createReadStream(file);
        readStream.pipe(writeStream);
        writeStream.on('data', function (chunk){
            console.log('Writing some data, just dont know what');
        });
        writeStream.on('end', function (filen) {
            console.log('Written file ' + filen.name);
        });
        writeStream.on('error', function (err) {
           console.log('Got the following error: ' + err);
        });

    });


}

我仍然遇到同样的问题,它等待数据库连接,连接没有错误,但查看输出(有一些其他代码获取文件名,设置集合并生成uID,它并行执行两个,但是,将它设置为只做一个没有区别)

Will create id 553f39448c80bd9e7e6d904e in the collection fsImage and store ./data/kick.jpg
Will create id 553f39448c80bd9e7e6d904f in the collection fsSound and store ./data/kick-808.wav
The bloody database is open!
And now lets write the thing
The bloody database is open!
And now lets write the thing

还没有开始写作

2 个答案:

答案 0 :(得分:0)

经过多次摆弄,以及人们比我更好的帮助,问题被发现了。 writeStream不会发出“结束”,只会发出“结束”,因此要修改代码,所有必须更改的内容都是

writeStream.on('end', function (filen) {
    console.log('Written file ' + filen.name);
});

writeStream.on('finish', function (filen) {
    console.log('Written file ' + filen.name);
});

答案 1 :(得分:0)

writeStream.on('close', function (file){ console.log(file.filename, 'write to database'); });

文件集合中的文件名是(filename)not(name)。 并使用关闭事件