Gabriel Gavrilov
Gabriel Gavrilov

Reputation: 361

"TypeError: GridFsStorage is not a constructor"

for some reason I keep getting a "TypeError: GridFsStorage is not a constructor" error. I have no idea why its giving me this error since I'm just following the official documentation.

Storage and upload

conn.once('open', ()=> {
    gfs = Grid(conn.db, mongoose.mongo)
    gfs.collection('uploads')
})

const storage = new GridFsStorage({
    url: DBURI,
    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 fileInfor = {
                    filename: filename,
                    bucketName: 'uploads'
                }
                resolve(fileInfo)
            })
        })
    }
})
const upload = multer({storage})

Requirement

const GridFsStorage = require('multer-gridfs-storage')

Bug

C:\Users\gabri\Desktop\GridFS\server.js:21
const storage = new GridFsStorage({
                ^

TypeError: GridFsStorage is not a constructor
    at Object.<anonymous> (C:\Users\gabri\Desktop\GridFS\server.js:21:17)      
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47

Upvotes: 6

Views: 7645

Answers (3)

Harry
Harry

Reputation: 11

This error occurs when an incompatible version of multer is used. Please downgrade to 1.4.4.

See comments for further information: Dependency problem using multer and multer-gridfs-storage

Upvotes: 1

jfriend00
jfriend00

Reputation: 708016

The doc on NPM shows this:

const {GridFsStorage} = require('multer-gridfs-storage');

which is the same as:

const GridFsStorage = require('multer-gridfs-storage').GridFsStorage;

Whereas you are using this which is not the same at all:

const GridFsStorage = require('multer-gridfs-storage');

Upvotes: 23

Abid Akash
Abid Akash

Reputation: 31

Use this:

import {GridFsStorage} from 'multer-gridfs-storage'

Upvotes: 3

Related Questions