node.js - mongoDB 中弃用错误,我应该在哪里传递参数?

标签 node.js express multer-gridfs-storage

我使用 mongoDB 存储 2 个文件,我收到这些消息:

弃用警告:当前 URL 字符串解析器已弃用,并将在未来版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。

弃用警告:当前的服务器发现和监控引擎已弃用,并将在未来版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。

这是我的代码,我不知道应该将这些选项传递到哪里:

var storageImage = new GridFsStorage({
  url: dbURI,
  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: "user_images"
        };
        resolve(fileInfo);
      });
    });
  }
});
const uploadImage = multer({ storage: storageImage });

var storageDoc = new GridFsStorage({
  url: dbURI,
  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: "user_cv"
        };
        resolve(fileInfo);
      });
    });
  }
});
const uploadDoc = multer({ storage: storageDoc });

//routes

router.post("/uploadImage", uploadImage.single("file"), (req, res) => {
  console.log(req.file);
  res.json({ imageId: req.file.id });
});

router.post("/uploadCV", uploadDoc.single("file"), (req, res) => {
  console.log(req.file);
  res.json({ cvId: req.file.id });
});

module.exports = router;

最佳答案

尝试这样。添加此行应该有效 options: { useNewUrlParser: true }

new GridFsStorage({
  url: dbURI,
  options: { useNewUrlParser: true },
  ...,
});

关于node.js - mongoDB 中弃用错误,我应该在哪里传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815850/

相关文章:

Node.js Express multer 使用 multer-s3 npm 将图像上传到 s3 时出现问题

node.js - NPM 在 docker 容器中升级后中断

node.js - 如何从 sails.js 中的多个集合中获取记录

javascript - 语法错误: Unexpected end of input in fetch API

node.js - Express + Socket.IO + RabbitMQ (node-amqp)

node.js - 如何将中间件移至服务层

node.js - Multer gridfs存储引用上传的文件到新集合

node.js - 错误 : grid. mongodb.GridStore 不是构造函数,使用 mongoose、Grid-fs-stream 和 grid multer 存储

node.js - 如何在 sequelize 中加入 4 个或更多表?

javascript - 解构分配在 node_modules meteor 中不起作用