【问题标题】:saving video/images to MongoDB in NodeJS server by using GridFS使用 GridFS 将视频/图像保存到 NodeJS 服务器中的 MongoDB
【发布时间】:2016-04-11 11:04:05
【问题描述】:

我正在寻找使用GridFS 将视频/图像存储在MongoDB 中的示例示例。我遇到了this official site 并按照下面的代码sn-p

var MongoClient = require('mongodb').MongoClient,
Grid = mongo.Grid;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
  if(err) return console.dir(err);
  var grid = new Grid(db, 'fs');
  var buffer = new Buffer("Hello world");
  grid.put(buffer, {metadata:{category:'text'}, content_type: 'text'}, function(err, fileInfo) {
    if(!err) {
      console.log("Finished writing file to Mongo");
    }
  });
});

当我运行代码时出现以下错误

Grid = mongo.Grid;
       ^
ReferenceError: mongo is not defined
at Object.<anonymous> (D:\Rahul\Nodejs\Fileupload\fileupload\Samples\grid-fs
\mongo.js:3:8)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3

请分享有关如何使用 Nodejs 中的 GridFS 在 MongoDB 中存储文件的任何链接。

编辑 1

我尝试了很多让它工作,最后我知道接口Grid 已从Mongodb 2.1 中删除,所以它不会工作,但没有关于替换 Grid 的文档。现在我不清楚要使用哪一个,即GridStoreGridFSBucket

此文档是否具有误导性?

【问题讨论】:

  • 讨厌说的很明显,mongo 定义了吗?我敢打赌不是。
  • @BanksySan 我不知道这是什么参考资料,我是从网站上拿来的,如果你知道的话,请你发布答案
  • 它告诉你第 3 行的变量 mongo 是未定义的。不能比这更明确了。
  • 谢谢@BanksySan,我只想知道 mongo 变量应该指向哪里,比如 var mongo = require('mongodb');

标签: mongodb gridfs gridfs-stream


【解决方案1】:

我相信你想要的是:

var mongo = require('mongodb');

require('mongodb'}:

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

来自文档here

【讨论】:

  • 感谢您的回答我试过这个并得到 Grid is not a function
猜你喜欢
  • 2012-11-15
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多