我在提交表单时遇到错误
Posted
技术标签:
【中文标题】我在提交表单时遇到错误【英文标题】:I am getting error on submitting the form 【发布时间】:2019-10-09 14:41:10 【问题描述】:在连接到 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 文件对象,连接成功
实际 - 必须打开数据库连接
【问题讨论】:
【参考方案1】://连接
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('问题') )
【讨论】:
我收到此错误 - C:\Users\charan puli\Desktop\upload\node_modules\gridfs-stream\lib\index.js:25 if (!db) throw new Error('missing db 参数\nnew Grid(db, mongo)'); ^ 错误:缺少 db 参数 new Grid(db, mongo)【参考方案2】:这应该对你有用,我已经测试过了:
//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 Driver API 的新 mongo 客户端的新 url 解析器有关。请参阅此参考:MongoClient
我遇到了同样的问题,这就是我最终回答您的问题的原因。
【讨论】:
C:\Users\charan puli\Desktop\upload\node_modules\gridfs-stream\lib\index.js:25 if (!db) throw new Error('missing db argument\nnew Grid(分贝,蒙戈)'); ^ 错误:缺少 db 参数 new Grid(db, mongo) 我想我犯了一个小错误。而不是:gfs = Grid(conn.db, mongoose.mongo);它应该是:gfs = Grid(conn, mongoose.mongo);以上是关于我在提交表单时遇到错误的主要内容,如果未能解决你的问题,请参考以下文章