Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoDB GridFS“;非法块格式';例外_Node.js_Mongodb_Gridfs - Fatal编程技术网

Node.js MongoDB GridFS“;非法块格式';例外

Node.js MongoDB GridFS“;非法块格式';例外,node.js,mongodb,gridfs,Node.js,Mongodb,Gridfs,我一直在Node.js中编写一个应用程序,将图像存储在MongoDB的GridFS文件系统中 我已通过应用程序上传了图像,图像似乎已正确存储: $ mongofiles -v -d speaker-karaoke get howard-basement-100x115.jpg Tue Jul 17 12:14:16 creating new connection to:127.0.0.1 Tue Jul 17 12:14:16 BackgroundJob starting: ConnectBG

我一直在Node.js中编写一个应用程序,将图像存储在MongoDB的GridFS文件系统中

我已通过应用程序上传了图像,图像似乎已正确存储:

$ mongofiles -v -d speaker-karaoke get howard-basement-100x115.jpg
Tue Jul 17 12:14:16 creating new connection to:127.0.0.1
Tue Jul 17 12:14:16 BackgroundJob starting: ConnectBG
Tue Jul 17 12:14:16 connected connection!
connected to: 127.0.0.1
done write to: howard-basement-100x115.jpg
这从MongoDB中抓取了.jpg,我可以毫无问题地打开它,所以看起来我上传的内容被正确存储了

但是,在我正在运行的应用程序中,当我尝试读取同一文件时,我会得到:

12:15:44 web.1     | started with pid 89621
12:15:45 web.1     | Connecting to mongodb://localhost/speaker-karaoke
12:15:45 web.1     | Speaker Karaoke express app started on 5000
12:15:48 web.1     | DEBUG: Get review thumbnail for 5005b7550333650000000001
12:15:48 web.1     | 
12:15:48 web.1     | node.js:201
12:15:48 web.1     |         throw e; // process.nextTick error, or 'error' event on first tick
12:15:48 web.1     |               ^
12:15:48 web.1     | Error: Illegal chunk format
12:15:48 web.1     |     at Error (unknown source)
12:15:48 web.1     |     at new <anonymous> (/Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongodb/lib/mongodb/gridfs/chunk.js:43:11)
12:15:48 web.1     |     at /Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:488:24
12:15:48 web.1     |     at Cursor.nextObject (/Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:462:5)
12:15:48 web.1     |     at [object Object].<anonymous> (/Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:456:12)
12:15:48 web.1     |     at [object Object].g (events.js:156:14)
12:15:48 web.1     |     at [object Object].emit (events.js:88:20)
12:15:48 web.1     |     at Db._callHandler (/Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1290:25)
12:15:48 web.1     |     at /Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:329:30
12:15:48 web.1     |     at [object Object].parseBody (/Users/hlship/workspaces/github/speaker-karaoke/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:118:5)
12:15:48 web.1     | process terminated
12:15:48 system    | sending SIGTERM to all processes
因此,它似乎连传递到store.open()的回调都没有

当您知道id而不知道文件名时,打开GridFS文件是否有问题

顺便说一句:

下面是它失败的功能:

var Chunk = exports.Chunk = function(file, mongoObject) {
  if(!(this instanceof Chunk)) return new Chunk(file, mongoObject);

  this.file = file;
  var self = this;
  var mongoObjectFinal = mongoObject == null ? {} : mongoObject;

  this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
  this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
  this.data = new Binary();

  if(mongoObjectFinal.data == null) {
  } else if(typeof mongoObjectFinal.data == "string") {
    var buffer = new Buffer(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data, 'binary', 0);
    this.data = new Binary(buffer);
  } else if(Array.isArray(mongoObjectFinal.data)) {
    var buffer = new Buffer(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data.join(''), 'binary', 0);
    this.data = new Binary(buffer);
  } else if(mongoObjectFinal.data instanceof Binary || Object.prototype.toString.call(mongoObjectFinal.data) == "[object Binary]") {    
    this.data = mongoObjectFinal.data;
  } else if(Buffer.isBuffer(mongoObjectFinal.data)) {
  } else {
    throw Error("Illegal chunk format");
  }
  // Update position
  this.internalPosition = 0;
};
解决方案

正在更新此处的解决方案,因为它在下面的注释中无法正确呈现

问题是重复;拥有两份mongodb和bson的副本,即使版本相同

幸运的是,mongoose将其所需的mongodb导出为属性mongo,因此我能够从我的
包.json中删除显式mongodb,并更改了:

mongo = require "mongodb"
mongoose = require "mongoose"
致:


现在情况看起来不错;我仍然认为模块系统需要一种认可的方式来访问依赖项(如果一个dep没有充分考虑到公开其dep的话)。

非常确定它是mongodb模块的副本(相同版本);删除node_模块和nmp安装似乎已经解决了这个问题。

您是用node/mongo编写GridFS文件,还是用其他方式?我是用node/mongo编写GridFS;如果有帮助的话,我可以展示这些代码,但事实上,mongofiles命令行工具可以正确读取文件内容,这让我觉得我做得很好。是的,只是仔细检查一下。GridFS没有真正的标准,所以我想知道这是否是驱动程序不一致。但是,由于您是使用节点驱动程序编写的,我认为这不是问题所在。更改了代码以基于ObjectID生成文件名。文件名是在为写入和读取创建GridStore时使用的。但是,这并没有改变行为,因此文件名(或缺少)不是真正的问题。现在好像在工作。我只是做了一些积极的npm工作;删除节点模块并重新运行npm安装。我知道我有两个相互竞争的mongodb模块副本,这可能导致了一个问题。我想进一步研究这件事。该死,它回来了。刚刚更新到mongoose@3.1.2及mongodb@1.1.7. mongodb有两个副本,但都是相同的版本。我可以验证mongofiles命令是否能够正确读取内容。好的,我想这次我真的解决了这个问题。我现在非常确定这是mongodb和bson模块的两个版本之间的冲突。请参阅上面的解决方案说明(我不得不将其移到那里,因为注释中的格式太有限)。
var Chunk = exports.Chunk = function(file, mongoObject) {
  if(!(this instanceof Chunk)) return new Chunk(file, mongoObject);

  this.file = file;
  var self = this;
  var mongoObjectFinal = mongoObject == null ? {} : mongoObject;

  this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
  this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
  this.data = new Binary();

  if(mongoObjectFinal.data == null) {
  } else if(typeof mongoObjectFinal.data == "string") {
    var buffer = new Buffer(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data, 'binary', 0);
    this.data = new Binary(buffer);
  } else if(Array.isArray(mongoObjectFinal.data)) {
    var buffer = new Buffer(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data.join(''), 'binary', 0);
    this.data = new Binary(buffer);
  } else if(mongoObjectFinal.data instanceof Binary || Object.prototype.toString.call(mongoObjectFinal.data) == "[object Binary]") {    
    this.data = mongoObjectFinal.data;
  } else if(Buffer.isBuffer(mongoObjectFinal.data)) {
  } else {
    throw Error("Illegal chunk format");
  }
  // Update position
  this.internalPosition = 0;
};
mongo = require "mongodb"
mongoose = require "mongoose"
mongoose = require "mongoose"
mongo = mongoose.mongo