Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
Atascado el error: Typeerror: grid.mongo.objectID no es un constructor

Estaba tratando de cargar imágenes en la base de datos mongodb. Y luego obtuve este error que no puedo resolver. ¿Alguien puede ayudar?

El código después de todas las declaraciones requeridas se muestra a continuación

 // MONGOOSE CONNECTION const conn = mongoose.createConnection(mongoURI); // GRID FS let gfs; conn.once('open' , ()=>{ // init stream gfs = Grid(conn.db , mongoose.mongo); gfs.collection('uploads'); }); // MULTER const storage = new GridFsStorage({ url: mongoURI, 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); }); }); } }); const upload = multer({storage}).single('file');

Luego, cuando traté de mostrar el objeto de archivo guardado en la base de datos usando gfs.file.find(), funcionó

 app.get("/files/:filename" , (req , res)=>{ gfs.files.findOne({filename : req.params.filename} , (err , file)=>{ if(!file){ return res.json({err : "No file exists"}); } return res.json(file) }) });

El código anterior funcionó y mostró el objeto json cuando se llamó. Pero cuando intenté mostrar la imagen no como json sino como la imagen real usando createReadStream, se rompió

 app.get("/image/:filename" , (req , res)=>{ gfs.files.findOne({filename : req.params.filename} , (err , file)=>{ if(!file){ return res.json({err : "No file exists"}); } // file exists // if it is an image if(file.contentType === 'image/jpeg' || file.contentType === 'image/png'){ // Read output to browser const readstream = gfs.createReadStream(file.filename); readstream.pipe(res); } else{ res.status(404).json({err : "not an image"}); } }); });
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!