TypeError:无法读取属性' ObjectID'未定义的

时间:2015-05-29 06:58:00

标签: javascript node.js objectid

我的代码如下,使用nodejs上传网页上的文件执行文件上传,服务器错误。

我试图直接将文件上传到Mongo。似乎gridform(https://github.com/aheckmann/gridform)是一个很好的解决方案。

节点v0.12.4

gridform 0.1.7

mongodb 2.0.33

    http = require("http");
    var mongo = require('mongodb');
    var url = require("url");
    var assert = require('assert');
    var formidable = require ( 'gridform/node_modules/formidable' ); 
    var gridfsStream = require ( 'gridform/node_modules/gridfs-stream' ); 

    var Db = require('mongodb').Db;
    var Server = require('mongodb').Server;
    var MongoDb = new Db('test', new Server('localhost', 27017), {w: 1});

    var app = http.Server(function(req, res){
        MongoDb.open(function(err, db) {
            var gridform = require('gridform');
            gridform.db = db;
            gridform.mongo = mongo;
            var form = gridform();
            assert(form instanceof formidable.IncomingForm);
            console.log(formidable.IncomingForm);
            form.on('fileBegin', function (name, file) {
                console.log('mmmeta');
                file.metadata = 'so meta'
            });
            form.parse(req, function (err, fields, files) {

            });
        });
    });
    app.listen(3000);

在文件上传的实现中,服务器错误

    /data/nodejs/node_modules/gridform/node_modules/gridfs-stream/lib/writestream.js:44
    this.id = new grid.mongo.BSONPure.ObjectID;
                                 ^
TypeError: Cannot read property 'ObjectID' of undefined
    at Stream.GridWriteStream (/data/nodejs/node_modules/gridform/node_modules/gridfs-stream/lib/writestream.js:44:38)
    at Grid.createWriteStream (/data/nodejs/node_modules/gridform/node_modules/gridfs-stream/lib/index.js:41:10)
    at IncomingForm.onPart (/data/nodejs/node_modules/gridform/lib/index.js:72:30)
    at MultipartParser.parser.onHeadersEnd (/data/nodejs/node_modules/gridform/node_modules/formidable/lib/incoming_form.js:393:10)
    at callback (/data/nodejs/node_modules/gridform/node_modules/formidable/lib/multipart_parser.js:102:31)
    at MultipartParser.write (/data/nodejs/node_modules/gridform/node_modules/formidable/lib/multipart_parser.js:208:9)
    at IncomingForm.write (/data/nodejs/node_modules/gridform/node_modules/formidable/lib/incoming_form.js:141:34)
    at IncomingMessage.<anonymous> (/data/nodejs/node_modules/gridform/node_modules/formidable/lib/incoming_form.js:110:12)
    at IncomingMessage.emit (events.js:107:17)
    at IncomingMessage.Readable.read (_stream_readable.js:373:10)

0 个答案:

没有答案