Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - 通过multer一次发布两个集合(Posting two collections at once with multer)

I have a problem with posting two collections - one is file, one is literal object with fields based on file.

(我在发布两个集合时遇到问题-一个是文件,一个是带有基于文件字段的文字对象。)

Here's the route method

(这是路线方法)

// @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()

});

and File model:

(和文件模型:)

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);

The problem is, that if I do it that way I can't get caption property and I have errors:

(问题是,如果我这样做,我将无法获得标题属性,并且会出现错误:)

TypeError: Cannot read property 'id' of undefined

(TypeError:无法读取未定义的属性“ id”)

or if I remove that

(或者如果我删除了)

req.file.id

(req.file.id)

I get

(我懂了)

TypeError: Cannot read property 'filename' of undefined

(TypeError:无法读取未定义的属性“文件名”)

even that I get good values in my database.

(即使我在数据库中获得了很好的价值。)

If I remove both id and filename I get caption property.

(如果同时删除ID和文件名,则将获得标题属性。)

So the problem is there:

(因此存在问题:)

    fileID: req.file.id,
    src: 'api/files/image/' + req.file.filename

but I don't know how to handle that.

(但我不知道该如何处理。)

  ask by Victor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I still don't know how to deal with it, but I did console log in post route method: // @route POST /upload // @desc Uploads file to DB

(我仍然不知道如何处理它,但是我在控制台上输入了post route方法:// @route POST / upload // @desc将文件上传到数据库)

router.post('/', upload.single('file'), auth, (req, res) => {
console.log(req.file);
  const newFile = new File({
    fileID: req.file.id,
    src: 'api/files/image/' + req.file.filename,
    altText: 'No image',
    caption: req.body.caption
  })
  newFile.save()

});

and I get:

(我得到:)

undefined
[0] { fieldname: 'file',
[0]   originalname: 'slide1.jpg',
[0]   encoding: '7bit',
[0]   mimetype: 'image/jpeg',
[0]   id: 5de06f65ff1bdb5388f1a44c,
[0]   filename: 'e872d1cafee8d191b256d27efc5ac94f.jpg',
[0]   metadata: null,
[0]   bucketName: 'files',
[0]   chunkSize: 261120,
[0]   size: 235600,
[0]   md5: '06ef6fba95a331af99276199589d3ed4',
[0]   uploadDate: 2019-11-29T01:07:50.105Z,
[0]   contentType: 'image/jpeg' }

So it looks that I have to await for request, because

(因此,看来我必须等待请求,因为)

req.file

(请求文件)

isn't yet defined, so I get error and it stop and don't post this req.body.caption.

(尚未定义,因此出现错误,它停止并且不发布此req.body.caption。)

That is what I suspect.

(这就是我所怀疑的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...