mongoDB中的弃用错误,我应该在哪里传递参数?

问题描述 投票:0回答:1

我正在使用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.js mongodb express
1个回答
1
投票

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

new GridFsStorage({
  url: dbURI,
  options: { useNewUrlParser: true },
  ...,
});
© www.soinside.com 2019 - 2024. All rights reserved.