Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error ReferenceError: ObjectID is not defined

I want to upload image with GridJS. I am using https://www.npmjs.org/package/gridfs-stream for this. It gives me error ReferenceError: ObjectID is not defined.

I am beginner to this, so have no idea whats going.

How to solve this error?

var Grid = require('gridfs-stream');
    var conn = mongoose.createConnection('localhost', 'Test', 27017);
    var gfs = Grid(conn.db, mongoose);

    var fileId = new ObjectID(uniqId);
    var fileType = req.header('X-File-Type');
    var fileName = req.header('X-File-Name');
    var uniqId = req.param('uniqId', '112211112111');

    console.log('upload start');
    // streaming to gridfs
    var writestream = gfs.createWriteStream(fileId, [{ "content_type": fileType, "metadata": { "fileName": fileName, "uploaded_at": (new Date()).toString() } }]);
    fs.createReadStream('/some/path').pipe(writestream);

    // streaming from gridfs
    var readstream = gfs.createReadStream(fileId, [{ "content_type": fileType, "metadata": { "fileName": fileName, "uploaded_at": (new Date()).toString() } }]);

    //error handling, e.g. file does not exist
    readstream.on('error', function (err) {
        console.log('An error occurred!', err);
        throw err;
    });

    readstream.pipe(response);
    console.log('upload done');
like image 647
Anup Avatar asked Mar 03 '14 09:03

Anup


1 Answers

Not directly a solution but a small typo wasted 10 minutes of my time:

var ObjectId = require('mongodb').ObjectID;

I declared a variable ObjectId with small d but when using the variable, I tried ObjectID with capital D.

like image 168
Illegal Argument Avatar answered Oct 16 '22 20:10

Illegal Argument