首页 > 解决方案 > mongoDB中的弃用错误,我应该在哪里传递参数?

问题描述

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

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

DeprecationWarning:当前的服务器发现和监控引擎已被弃用,并将在未来的版本中删除。要使用新的服务器发现和监控引擎,请将选项 { 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;

标签: node.jsexpressmulter-gridfs-storage

解决方案


像这样试试。添加这一行应该可以工作options: { useNewUrlParser: true }

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

推荐阅读