0
votes

I am using gridfs and mutler to upload files likes ppt pdf etc..to mongodb atlas. When setting up router its keeps saying db not found I have my uri in a config file.What am i missing here in mongodb atlas i have 3 collections in a database called 'test' do i have to create another db in the cluster? Please help

if (!db) throw new Error('missing db argument\nnew Grid(db, mongo)'); ^

Error: missing db argument new Grid(db, mongo) at new Grid (/Users/ezana/Desktop/Canvas/backend/node_modules/gridfs-stream/lib/index.js:25:18) at Grid (/Users/ezana/Desktop/Canvas/backend/node_modules/gridfs-stream/lib/index.js:19:12) at Object. (/Users/ezana/Desktop/Canvas/backend/api/routes/fileUploadRoutes.js:9:11) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (/Users/ezana/Desktop/Canvas/backend/app.js:7:21) at Module._compile (internal/modules/cjs/loader.js:701:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) at Module.load (internal/modules/cjs/loader.js:600:32) at tryModuleLoad (internal/modules/cjs/loader.js:539:12) at Function.Module._load (internal/modules/cjs/loader.js:531:3)

const router = require('express').Router();
const multer = require('multer');
const db = require('./config/main').mongoURI;
const {
    mongo,
    connection
} = require('mongoose');
const Grid = require('gridfs-stream');
Grid.mongo = mongo;
const gfs = Grid(connection.db);

const storage = require('multer-gridfs-storage')({
    db: connection.db,
    file: (req, file) => {
        return {
            filename: file.originalname
        }
    }
});
1

1 Answers

0
votes

const mongoose = require("mongoose");
const multer = require("multer");
const Grid = require("gridfs-stream");
const GridFsStorage = require("multer-gridfs-storage");
const db = mongoose.connection;

let gfs;
db.once("open", function() {
  gfs = Grid(db.db, mongoose.mongo);
});



const storage = new GridFsStorage({
  url:
    url,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, (err, buf) => {
        if (err) {
          return reject(err);
        }
        const filename = buf.toString("hex") + path.extname(file.originalname);
        const fileInfo = {
          filename: filename,
          bucketName: "uploads"
        };
        resolve(fileInfo);
      });
    });
  }
});

This worked for me.