尝试使用gridfs,但某些内容输出不正确

时间:2013-08-20 05:25:53

标签: node.js mongodb mongoose gridfs

嘿所以我正在尝试使用gridfs来存储个人资料图片...我不确定我做错了什么,我对此也很陌生。

这是我到目前为止所得到的,但似乎是错误的:

TypeError: Cannot read property 'primary' of undefined
    at Stream.GridStore.open (..\node_modules\mongodb\lib\mongodb\gridfs\gridstore.js:146:69)

这是我的代码:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.Types.ObjectId;
var GridStore = require('mongodb').GridStore;
var Step = require('step');


exports.newProfilePicturePost = function(req, res) {
    var db = mongoose.createConnection('mongodb://localhost/test5');
    var fileId = new ObjectId();
    var gridStore = new GridStore(db, fileId, "w", {
        "metadata": {
            category: 'image'
        }, content_type: 'image'});
    gridStore.chunkSize = 1024 * 256;

    gridStore.open(function(err, gridStore) {
     Step(
       function writeData() {
         var group = this.group();

         for(var i = 0; i < 1000000; i += 5000) {
           gridStore.write(new Buffer(5000), group());
         }
       },

       function doneWithWrite() {
         gridStore.close(function(err, result) {
           console.log("File has been written to GridFS");
         });
       }
     )
    });

};

我应该对我的代码做出任何帮助或修复,我也不知道如何指定我想要存储图片的集合,我还想将userId添加到集合中保存的图片中稍后取出它。

0 个答案:

没有答案