连接到mongodb时,我收到警告
“ DeprecationWarning:当前URL字符串解析器已被弃用,并且将 在将来的版本中删除。要使用新的解析器,请传递选项{ useNewUrlParser:true}到MongoClient.connect。”
提交表单时-我得到了
必须打开数据库连接才能存储文件 在GridFSStorage._handleFile(C:\ Users \ charan puli \ Desktop \ upload \ node_modules \ multer-gridfs-storage \ lib \ gridfs.js:341:17) 在C:\ Users \ charan puli \ Desktop \ upload \ node_modules \ multer \ lib \ make-middleware.js:144:17 在allowAll(C:\ Users \ charan puli \ Desktop \ upload \ node_modules \ multer \ index.js:8:3) 在wrappedFileFilter(C:\ Users \ charan puli \ Desktop \ upload \ node_modules \ multer \ index.js:44:7) 在Busboy。 (C:\ Users \ charan puli \ Desktop \ upload \ node_modules \ multer \ lib \ make-middleware.js:114:7) 在Busboy.emit(events.js:182:13)
//////////////////app.js///////////////////////
//middle wares
app.use(bodyparser.json())
app.use(methodOverride('_method'))
app.set("view engine","ejs")
//connection
var mongoURI='mongodb+srv://user:password@clusterpuli-xs9yc.mongodb.net/test?retryWrites=true'
mongoose.connect(mongoURI,{useNewUrlParser:true})
.then(()=>{console.log('connected successfully');
})
.catch(err=>{console.log(err);
})
var conn=mongoose.connection
var gfs
conn.once('open',() =>{
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('questions')
})
//create storage object
var storage = new GridFsStorage({
url: mongoURI,
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: 'questions'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
//@route /upload POST
app.post('/upload',upload.single('file'),(req,res)=>{
res.json({'file':req.file})
})
var port= 3000
app.get("/",(req,res)=>{
res.render('index')
})
app.listen(port,()=>{
console.log(`app is running at ${port}`);
})
////////////////////////////////////
预期-json文件对象,已成功连接
实际-数据库连接必须打开
答案 0 :(得分:0)
//连接
var mongoURI ='mongodb + srv:// charanpuli:Charan @ 1999 @ clusterpuli-xs9yc.mongodb.net / test?retryWrites = true'
var conn = mongoose.connection
MongoClient.connect(db,{useNewUrlParser:true})
.then(()=>{console.log('connected successfully');
})
.catch(err=>{console.log(err);
})
var gfs conn.once('open',()=> { gfs = Grid(conn.db,mongoose.mongo);
gfs.collection('questions') })
答案 1 :(得分:0)
这应该对您有用,我已经对其进行了测试:
//connection
const mongoURI = 'mongodb+srv://user:password@clusterpuli-xs9yc.mongodb.net/test?retryWrites=true';
const promise = mongoose.connect(mongoURI, { useNewUrlParser: true });
const conn = mongoose.connection;
let gfs;
conn.once('open',() => {
gfs = Grid(conn, mongoose.mongo);
gfs.collection('questions');
});
//create storage object
const storage = new GridFsStorage({
db: promise,
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: 'questions'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
有关更多信息,请参见此处的参考资料: Multer's GridFS storage engine
它与Node.js MongoDB驱动程序API的新mongo客户端的新url解析器有关。请参阅此参考:MongoClient
我遇到了同样的问题,这就是为什么我最终回答了你的问题。