代码:
var test_id = new ObjectID();
var gridStore = new GridStore(mongoose.connection.db, test_id , 'test_gs_getc_file' , 'w');
gridStore.open(function(err, gridStore){
var data = fs.readFileSync('./test.jpg');
gridStore.write(data, function(err, doc){
gridStore.close(function(err, doc){
GridStore.read(mongoose.connection.db, test_id , function(err, data){
if(err){
console.log("ERROR", err);
}else{
console.log("FILE data", data);
}
})
})
})
})
该文件正在保存在fs.chunks
和fs.files
集合中。但是当我尝试读取文件时,它给出了Illegal chunk format
错误。
我试过了
- writeFile 方法,但它会导致相同的错误
- 重新安装了 mongodb,但它导致同样的错误
完整的错误信息:
C:\Users\rajsubramanian\nodeapps\populate\node_modules\mongoose\node_modules\mon
godb\lib\mongodb\connection\server.js:594
throw err;
^
Error: Illegal chunk format
at Error (unknown source)
at new exports.Chunk (C:\Users\rajsubramanian\nodeapps\populate\node_modules
\mongodb\lib\mongodb\gridfs\chunk.js:43:11)
at nthChunk (C:\Users\rajsubramanian\nodeapps\populate\node_modules\mongodb\
lib\mongodb\gridfs\gridstore.js:582:24)
at Cursor.nextObject (C:\Users\rajsubramanian\nodeapps\populate\node_modules
\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:680:5)
at Cursor.nextObject.commandHandler (C:\Users\rajsubramanian\nodeapps\popula
te\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:658:14)
at Db._executeQueryCommand (C:\Users\rajsubramanian\nodeapps\populate\node_m
odules\mongoose\node_modules\mongodb\lib\mongodb\db.js:1659:20)
at g (events.js:192:14)
at EventEmitter.emit (events.js:126:20)
at Server.Base._callHandler (C:\Users\rajsubramanian\nodeapps\populate\node_
modules\mongoose\node_modules\mongodb\lib\mongodb\connection\base.js:410:25)
at Server.connect.connectionPool.on.server._serverState (C:\Users\rajsubrama
nian\nodeapps\populate\node_modules\mongoose\node_modules\mongodb\lib\mongodb\co
nnection\server.js:580:20)
DEBUG: Program node app exited with code 1