Shanthi B
Shanthi B

Reputation: 143

TypeError: mongodb_1.ObjectID is not a constructor with GridFsStorage

I'm new to nodejs along with mongoDB, can anyone please help me to solve this issue. i used to multer, multer-gridfs-storage, gridfs-stream for uploading file and images. But i'm getting this TypeError: mongodb_1.ObjectID is not a constructor in resolve() inside the promise in GridFsStorage. i have shared the code below.

const express  = require('express');
const app = express();
require('dotenv').config()
const PORT = process.env.PORT;
const morgan = require('morgan');
const multer = require('multer');
const {GridFsStorage} = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const path = require('path');
const crypto = require('crypto');

const MongoURI = "atlas url";


app.use(methodOverride('_method'));
app.use(express.json())
app.use(morgan(':method :status :url'));

const connectDB = mongoose.createConnection(MongoURI,{useUnifiedTopology:true,useFindAndModify:false, useNewUrlParser:true,useCreateIndex: true})

let gfs;

connectDB.once('open',()=>{
    console.log("dbConnected....")
    gfs =  Grid(connectDB.db,mongoose.mongo)
    gfs.collection('uploads');
})

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

 


app.listen(PORT,()=>{
    console.log(`port connected....${PORT}`)
})

const upload = multer({storage:storage})

app.post('/uploadfile',upload.single('file'),async(req,res)=>{
   res.json({
       file:req.file
   })
})

enter image description here

Upvotes: 3

Views: 6807

Answers (3)

Navdeep Singh
Navdeep Singh

Reputation: 11

Visit https://github.com/devconcept/multer-gridfs-storage/issues/352#issue-946945336 Changing the method name from ObjectID() to ObjectId() of mongodb_1 class worked for me, looks like a compatibility issue in gridfs.js file in node_modules.

Upvotes: 1

Carlos Jacinto
Carlos Jacinto

Reputation: 66

You must call mongodb.ObjectId and not ObjectID, and then you can have a const with that name. But with ObjectId everything will work. Keep on coding.

Upvotes: 2

lo0k
lo0k

Reputation: 151

My solution is to change the mongodb_1.ObjectID() inside its node module's gridfs.js file into mongodb_1.ObjectI"d"() and everything works. I guess the author is writing for the old version of mongodb, but in my case it's the newer version so "d" should be lowercase.

Update:

Someone just post an issue to the author's github, you can follow this page for official solutions.

Upvotes: 6

Related Questions