2016-04-11 82 views
0

我正在尋找一個使用GridFSMongoDB中存儲視頻/圖像的示例示例。我碰到this official site並遵循下面的代碼片斷使用GridFS在NodeJS服務器中將視頻/圖像保存到MongoDB中

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 

請分享任何鏈接,如何將文件存儲在使用GridFS的中的NodeJS MongoDB中。

編輯1

我嘗試了很多得到它的工作,最後我才知道接口GridMongodb 2.1刪除,這樣就不會工作,但沒有用於替換電網的文檔。現在我不清楚要使用哪一個,即GridStore,GridFSBucket

本文檔是否有誤導性?

+0

討厭陳述明顯,是'mongo'定義的?我敢打賭,事實並非如此。 – BanksySan

+0

@BanksySan我沒有任何想法是什麼樣的參考是,我把它從網站,因爲它是從網站,你可以請張貼的答案,如果你知道 –

+0

它告訴你,變量'mongo',第3行是undefined 。不能比這更明確。 – BanksySan

回答

1

我相信你想要的是:

​​

關閉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

+0

感謝您的回答我試過這個,並得到網格是不是一個功能 –

+0

請檢查編輯 –

相關問題