首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:无法读取未定义(读取'findOne')节点js gridFS的属性

TypeError:无法读取未定义(读取'findOne')节点js gridFS的属性
EN

Stack Overflow用户
提问于 2022-11-21 08:48:46
回答 1查看 28关注 0票数 0

我有gfs方法从mongoDB获取图像,代码在主app.js时运行良好,但当我试图将它移动到一个文件夹时,它返回TypeError: Cannot read properties of undefined (reading 'findOne')

我的图片/猫鼬模型:

代码语言:javascript
运行
复制
const mongoose = require('mongoose');
const mongoURI = process.env.MONGOURI;
const crypto = require("crypto");
const multer = require("multer");
const {GridFsStorage} = require("multer-gridfs-storage");
const Grid = require("gridfs-stream");
const path = require('path');


const conn = mongoose.createConnection(mongoURI);

//Init gfs
let gridfsBucket; 

let gfs = conn.once('open', () => {
  gridfsBucket = new mongoose.mongo.GridFSBucket(conn.db, {
  bucketName: 'uploads'
});

  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection('uploads');
  return gfs;
})



//create Storage engine
const storage = new GridFsStorage({
  limits: { fileSize: 5 * 1000 * 1000 },
  url: mongoURI,
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(8, (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 });


module.exports = {
    upload,
    gridfsBucket,
    gfs,
    conn
}

我的图像路径:

代码语言:javascript
运行
复制
const {gridfsBucket} = require('../models/mongoose');
const gfs = require('../models/mongoose');


  exports.getImg = (req, res) => {
    console.log(`req file name is: ${req.params.filename}`);
    gfs.files.findOne({filename: req.params.filename}, (err, file) => {

      //Check if file exist
      if(!file || file.length === 0) {
        return res.status(404).json({
          err: "No file exist"
        });
      } 
        // Check if image
      if(file.contentType === "image/jpeg" || file.contentType === "image/png" || file.contentType === "image/jpg" || file.contentType === "image/heic") {
        //Read output to client
        const readstream = gridfsBucket.openDownloadStreamByName(file.filename);
        readstream.pipe(res);
      } else {
        res.status(404).json({err: "Not an image"});
      }
    });
  };

错误:

代码语言:javascript
运行
复制
TypeError: Cannot read properties of undefined (reading 'findOne')
    at exports.getImg (/Users/dordadon/Desktop/Full stack course/NerZikaron/controllers/system.js:136:15)
    at Layer.handle [as handle_request] (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/layer.js:95:5)
    at /Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/index.js:284:15
    at param (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/index.js:365:14)
    at param (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/index.js:376:14)
    at Function.process_params (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/index.js:421:3)
    at next (/Users/dordadon/Desktop/Full stack course/NerZikaron/node_modules/express/lib/router/index.js:280:10)```
EN

Stack Overflow用户

发布于 2022-11-21 08:56:27

根据语法,gfs不是您所期望的。您正在尝试访问模块中的gfs。试一试:

代码语言:javascript
运行
复制
const {gfs} = require('../models/mongoose');
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74516252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档