我使用CollectionFS来处理表单的文件上传。但是,现在除非上传文件,否则表单将不会提交(我希望文件上传是可选的)。
以下是表单的代码:
Template.postForm.events({
// handle the form submission
'submit form': function(event) {
// stop the form from submitting
event.preventDefault();
// get the data we need from the form
var file = $('.myFileInput').get(0).files[0];
var fileObj = Images.insert(file);
var currentCalendar = this._id;
var newPost = {
postDate: Date.parse(event.target.postDate.value),
postDateDisplay: event.target.postDate.value,
postType: event.target.postType.value,
postContent: event.target.postContent.value,
calendarId: currentCalendar,
owner: Meteor.userId(),
postImage: fileObj,
};
// create the new poll
Posts.insert(newPost);
// Clear form
event.target.postDate.value = "";
event.target.postType.value = "";
event.target.postContent.value = "";
event.target.postImage.value = "";
}
});
以下是我在JavaScript控制台中收到的错误:
Uncaught Error: FS.File.attachData requires a data argument with some data