Reputation: 9746
I am using following code to create a file in mongodb. I get no error. But when i chech my Test
database nothing is seen there uploaded. What am i doing wrong?
var GridStrm = require('gridfs-stream');
console.log('Logo Upload');
var conn = mongoose.createConnection('localhost', 'Test', 27017);
var gfs = GridStrm(conn.db, mongoose.mongo);
var writeStream = gfs.createWriteStream('file.txt');
fs.createReadStream('d:\\somepath').pipe(writestream);
I get the error Error: EISDIR, read
.
I got new error :-
events.js:72
throw er; // Unhandled 'error' event
^
Error: Cannot determine state of server
at canCheckoutReader
I further tried the following code :-
req.pipe(gfs.createWriteStream({
filename: 'test'
}));
res.send("Success!");
But still no result.
Upvotes: 0
Views: 3743
Reputation: 151190
A more minimal example
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
var fs = require('fs');
var conn = mongoose.createConnection('localhost', 'test', 27017 );
conn.once('open', function() {
var gfs = Grid( conn.db, mongoose.mongo );
var source = fs.createReadStream('file.txt');
var target = gfs.createWriteStream({
filename: 'file.txt'
});
source.pipe(target);
});
This puts into the collection:
> db.fs.files.find()
{
"_id" : ObjectId("531470dc8b79087430b2e38d"),
"filename" : "file.txt",
"contentType" : "binary/octet-stream",
"length" : 138,
"chunkSize" : 262144,
"uploadDate" : ISODate("2014-03-03T12:09:00.896Z"),
"aliases" : null,
"metadata" : null,
"md5" : "87be58f21745384bcd13743bde9ae8fb"
}
So your problem was:
In all the gridFS methods the filename is just a label that is sent to the server. Other methods are used to work with real files. So if you are still confused, try the code then change this line afterwards:
var target = gfs.createWriteStream({
filename: 'dont-exist.txt'
});
Your same file content will be used, just with a new label on the server.
Upvotes: 1