You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
whenever I try to use gfs.createReadStream I keep getting this error
...\node_modules\gridfs-stream\lib\readstream.js:68
this._store = new grid.mongo.GridStore(grid.db, this.id || new grid.mongo.ObjectID(), this.name, this.mode, options);
^
TypeError: grid.mongo.ObjectID is not a constructor
I don't know where's the mistake I made I tried to search everywhere but I'm not found any solution here's my code
constexpress=require('express');constrouter=express.Router();constmulter=require("multer");const{ GridFsStorage }=require('multer-gridfs-storage');const{ connection }=require('../db/connection');constcreateError=require('http-errors');constmongoose=require("mongoose")constGrid=require("gridfs-stream")constgfsStorage=newGridFsStorage({db: connection,file: (req,file)=>{letextname=path.extname(file.originalname);//handle the file name changesconstoriginalname=Buffer.from(file.originalname,"latin1").toString("utf8");return{filename: `${generateRandomId()}--${originalname}`};},});constgfsUpload=multer({storage: gfsStorage});constgfs=Grid(connection,mongoose.mongo);gfs.collection('fs')router.post("/gridfs",gfsUpload.single("file"),function(req,res,next){let{ filename, chunkSize, id }=req.file;console.log(req.file)res.status(200).json({file: { filename, id }});})router.get("/gridfs/:filename",asyncfunction(req,res,next){constgfsFiles=connection.collection("fs.files");console.log(req.params.filename);constfile=awaitgfsFiles.findOne({filename: req.params.filename});if(!file)returnnext(createError(404));res.setHeader("Content-Disposition",`attachment; filename=${file.filename}`);res.setHeader("Content-Type",file.contentType);gfs.createReadStream({filename: file.filename}).pipe(res)})
if you are asking what's inside the '../db/connection' here's the code
constmongoose=require("mongoose");const{ mongoURI }=require("../configurations/appConfig")mongoose.set('strictQuery',true);mongoose.connect(mongoURI,{useNewUrlParser: true,useUnifiedTopology: true,});constconnection=mongoose.connection;connection.on("error",(e)=>{console.error(e);});connection.once("open",()=>{console.log("connected to db");});exports.connection=connection
I hope to found a solution for that
The text was updated successfully, but these errors were encountered:
whenever I try to use
gfs.createReadStream
I keep getting this errorI don't know where's the mistake I made I tried to search everywhere but I'm not found any solution here's my code
if you are asking what's inside the
'../db/connection'
here's the codeI hope to found a solution for that
The text was updated successfully, but these errors were encountered: