1

嘿,所以我正在尝试使用 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 添加到保存在集合中的图片以供以后获取.

4

0 回答 0