我在发布两个集合时遇到问题-一个是文件,一个是带有基于文件字段的文字对象。这是路线方法
// @route POST /upload
// @desc Uploads file to DB
router.post('/', upload.single('file'), auth, (req, res) => {
const newFile = new File({
fileID: req.file.id,
src: 'api/files/image/' + req.file.filename,
altText: 'No image',
caption: req.body.caption
})
newFile.save()
});
和文件模型:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const FileSchema = new Schema({
fileID: {
type: Schema.Types.ObjectId
},
src: {
type: String
},
altText: {
type: String
},
caption: {
type: String
}
});
module.exports = File = mongoose.model('file', FileSchema);
问题是,如果我这样做,我将无法获得标题属性,并且出现错误:
TypeError:无法读取未定义的属性'id'
或者如果我删除了它,则>
req.file.id
我知道
TypeError:无法读取未定义的属性'文件名'
即使我在数据库中获得了良好的价值。如果同时删除ID和文件名,则将获得标题属性。因此存在问题:
fileID: req.file.id, src: 'api/files/image/' + req.file.filename
但我不知道该如何处理。
我在发布两个集合时遇到问题-一个是文件,一个是带有基于文件字段的文字对象。这是route方法// // @route POST / upload // @desc将文件上传到DB router.post('/ ...
我仍然不知道该如何处理,但是我在后路由方法中做了控制台登录:// @route POST /上传// @desc将文件上传到数据库