aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

Can not get fileinfo sometimes after piped the file to the mongo #55

Closed char1st closed 9 years ago

char1st commented 9 years ago

var MongoClient = mongo.MongoClient; MongoClient.connect("mongodb://127.0.0.1:27017/test",function(err,db){ var gfs=Grid(db,mongo); var filename='tmp.json'; fs.createReadStream(filename).pipe(gfs.createWriteStream({filename:filename})); gfs.files.find({filename:filename}).toArray(function(err,docs){console.log(docs)}); });

The docs is empty sometimes . Why ? Do i used the gridfs-stream in a wrong way ?

char1st commented 9 years ago

... I found the reson

The gfs.files.find must be called at on gfs.createWriteStream() close.

like that

fs.createReadStream(filename).pipe(gfs.createWriteStream({filename:filename}).on('close',function(){ gfs.files.find({filename:filename}).toArray(function(err,docs){console.log(docs)}); }));