1

I'm writing a simple program that will handle MP3 file uploads directly from a designated directory for testing, and later by parsing form data. Right now I am having an issue with providing the proper information to the MongoDB GridFS system Node Driver and Node Streams API that would allow me to initialize an instance of the GridFSBucket to begin streaming files into the db. The mongo connection starts in the mongoObject.js file where some values are inserted to verify data can flow to the mLab db and then that connection is exported throughout the program. When attempting to upload I receive the error

 " _chunksCollection: db.collection(options.bucketName + '.chunks'),  
 TypeError: db.collection is not a function"

Thank you for taking the time to assist, below is my code.

mongoObject.js

var mongodb = require('mongodb'),
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
   // GridStore = require('mongodb').GridStore,
    GridFSBucket = require('mongodb').GridFSBucket,
    Code = require('mongodb').Code,
   // Grid = require('gridfs-stream'),
    gridform = require('gridform'),
    assert = require('assert'),
    mongoURI =  'MLAB-URI-HERE',
    test = require('assert');

const dbName = 'crate';
const testUsername = 'user1';

 module.exports.startup = function(callback) {

  console.log('startup');

   MongoClient.connect(mongoURI, function(err, client) {
       // Create a collection we want to drop later
      const col = client.db(dbName).collection(testUsername);
       // Insert a bunch of documents
      col.insert([{ a: 31, b: 31 }, { a: 13, b: 13 }], { w: 1 }, 
      function(err, result) {
           test.equal(null, err);
           console.log("insert");
       });

       db = client.db(dbName);



   });



}

index.js

const { Readable } = require('stream');

var mongodb = require('mongodb'),
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
   // GridStore = require('mongodb').GridStore,
    GridFSBucket = require('mongodb').GridFSBucket,
    Code = require('mongodb').Code,
   // Grid = require('gridfs-stream'),
    gridform = require('gridform'),
    assert = require('assert'),
    mongoURI =  'MLAB-URI-HERE',
    test = require('assert');


var db = require('./mongoObject.js');


var ct = './ct.mp3';

function upload(db, ct) {


db = db.client.db(dbName);

console.log("starting bucket");
let bucket = new GridFSBucket({
    bucketName: 'user2'
});


const readableTrackStream = new Readable();
readableTrackStream.push(ct);
readableTrackStream.push(null);

let uploadStream = bucket.openUploadStream(ct);
readableTrackStream.pipe(uploadStream)

uploadStream.on('data', (chunk) => {
    console.log('data')
})

uploadStream.on('error', (chunk) => {
    console.log('err');
})

uploadStream.on('end', () => {
    console.log('end');
})

}

upload(db, ct);

package.json dependencies

 "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.3",
    "express-handlebars": "^3.0.0",
    "formidable": "^1.2.1",
    "gridform": "^0.1.7",
    "gridfs": "^1.0.0",
    "gridfs-stream": "^1.1.1",
    "handlebars": "^4.0.11",
    "mongodb": "^3.0.5",
    "nodemon": "^1.17.2",
    "path": "^0.12.7",
    "stream": "0.0.2",
    "underscore": "^1.8.3"
}
  • `var db = require('./mongoObject.js');` does not return a valid connection reference. Look at [How to properly reuse connection to Mongodb across NodeJs application and modules](https://stackoverflow.com/questions/24621940/how-to-properly-reuse-connection-to-mongodb-across-nodejs-application-and-module) for some of the valid approaches to doing that. – Neil Lunn Apr 16 '18 at 01:46

0 Answers0