我使用 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/