使用 multer gridfs 存储上传的文件我想将它引用到新创建的集合(电影)。我引用上传文件的文件 ID 与上传文件不同,即使电影集合没有保存到数据库中。我上传文件引用的文件ID与上传文件不同,即使电影集合没有保存在数据库中
//movie schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MovieSchema = new Schema({
description: {
type: String,
},
category: {
type: String,
},
token: {
type: String,
},
fileID: {
type: Schema.Types.ObjectId,
ref: "contents.files"
}
});
module.exports = movies = mongoose.model('movies', MovieSchema);
let gfs;
conn.once('open', () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('contents');
});
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename = req.body.fileName + path.extname(file.originalname);
const Description = req.body.Description
const fileInfo = {
filename: filename,
bucketName: 'contents',
metadata: req.body,
}
resolve(fileInfo, Description);
});
}
});
const upload = multer({
storage
});
router.get('/', (req, res) => {
res.render('index');
console.log(req.body)
});
//** uploading file to the db */
router.post('/', upload.any(), (req, res) => {
const movie = new movies({
description: "test",
category: "test",
fileID: gfs.files.id
})
movie.save()
});
fileID 与上传的文件 ID 不同,并且集合未保存在数据库中 fileID 与上传的文件 ID 不同,并且集合未保存在数据库中 fileID 与上传的文件 ID 不同,并且集合不保存在数据库中