我正在尝试上传媒体文件和一些字符串,但连接超时。
我想将带有文件详细信息的图像、视频等媒体文件上传到 MongoDB。
上传媒体文件时,我输入的详细信息没有。(媒体文件的描述和名称)
我得到的错误是
[在此处输入图像描述][1]
MongooseError: Operation `mediainfos.insertOne()` buffering timed out after 10000ms at Timeout.<anonymous>
我正在使用的代码是
const conn = mongoose.createConnection(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
},(err,database)=>{
db = database
})
let gfs;
conn.once("open", () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection("uploads");
});
const 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: "uploads",
};
resolve(fileInfo);
});
});
},
});
const upload = multer({storage});
//Upload Files to DB
app.post('/upload',upload.single('file'),async (req,res)=>{
const fileName = req.file != null ? req.file.filename :null
console.log("Media Name "+req.body.medianame)
const MediaInfo = new mediaInfo({
media_id:fileName,
media_name:req.body.medianame,
media_description:req.body.mediadescription
})
try{
const NewMediaInfo = await MediaInfo.save()
res.send("Works Fine")
}catch(error){
console.log(error)
}
res.redirect('/');
});
MediaInfo 是架构
const Mongoose = require("mongoose");
const Schema = Mongoose.Schema
let mediaInfo = new Schema({
media_id:{
type: String
},
media_name:{
type:String
},
media_description:{
type:String
},
});
module.exports = Mongoose.model('mediaInfos',mediaInfo)
错误图像 [1]:https ://i.stack.imgur.com/DqMdC.png