节点:8940DeprecationWarning:GridStore已弃用,并将在以后的版本中删除请改用GridFSBucket

问题描述

我遇到此错误:(node:8940)DeprecationWarning:GridStore已弃用,并将在以后的版本中删除。请改用GridFSBucket
和idk关于相同的一些细节我已经做了一些修改,但是仍然向我显示错误,有人告诉我该怎么做 这是我的控制器文件

const logger = require('../libs/loggerLib');

/* Uploading a file */

let uploadFile = (req,res) => {
    return res.status(200).json(global.uploadedFileInfo);
    console.log(global.uploadedFileInfo);
}

/* fetching the file */

    let fetchFile = (req,res) => {
        try {
            global.GridFSBucket.files.findOne({ filename: req.params.filename },(err,file) => {
              if (err) {
                logger.error(err,'fileController:getFile()');
                return res.status(500).json({ err: 'Internal server error' });
              }
              if (!file || file.length === 0) {
                return res.status(404).json({ err: 'No file exists' });
              }
              const readstream = global.GridFSBucket.createReadStream(file.filename);
              readstream.pipe(res);
            });
          } catch (err) {
            logger.error(err,'fileController:getFile()');
            return res.status(500).json({ err: 'Internal server error' });
          }
        
    
    }
     
    module.exports{
    uploadFile : uploadFile,fetchFile : fetchFile 
    }

这是我的连接文件

const appConfig = require("../../config/appConfig");
const multer = require('multer');
const GridFsstorage = require('multer-gridfs-storage');
const path = require('path');
const crypto = require('crypto');


  // Create storage engine
  const storage = new GridFsstorage({
    url: appConfig.db.uri,file: (req,file) => {
      return new Promise((resolve,reject) => {
        crypto.randomBytes(16,buf) => {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString('hex') + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,bucketName: 'uploads',originalFileName: file.originalname
          };
          global.uploadedFileInfo = fileInfo;
          resolve(fileInfo);
         
        });
      });
    }
  });

let upload = multer({ storage });

module.exports = { upload: upload };

这是我的路线文件

const mongoose = require('mongoose');
    const Grid = require('gridfs-stream');
    const appConfig = require("../../config/appConfig");
    const attachments = require('../middlewares/attachments');
    const attachmentController = require('../controllers/attachments')
    
    //making connection with mongoose
    const connection = mongoose.createConnection(appConfig.db.uri);
    connection.once('open',() => {
      global.GridFSBucket = Grid(connection.db,mongoose.mongo);
      global.GridFSBucket.collection('uploads');
    });
    
    let setRouter = (app) => {
      let baseUrl = `${appConfig.apiVersion}/file`;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)