我有一个表格,其中coverImage和附件是可选的。但是,目前用户必须填写所有表单。如果不是,流星打印警告:流星:只有当值存在时才将字段插入到MongoDB中
Uncaught ReferenceError: imageIdVar is not defined
我明白这个错误信息来自哪里。
那么,如何在将文档插入到集合中时使字段可选?
我的模板助手:
Template.adminNewsEvents.events({
'change #coverImage': function(evt, temp) {
/* FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
if (err) throw err;
});
}); */
var image = event.target.files[0];
// Insert the image into the database
// getting the image ID for use in the course object
var imageObject = Images.insert(image);
// The image id is stored in the image object
var imageId = imageObject._id
// Create a reactive var to be used when the course is added
imageIdVar = new ReactiveVar(imageId);
},
'change #attachment': function(evt, temp) {
/* FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
if (err) throw err;
});
}); */
var attachment = event.target.files[0];
// Insert the image into the database
// getting the image ID for use in the course object
var attachmentObject = Attachments.insert(attachment);
// The image id is stored in the image object
var attachmentId = attachmentObject._id
// Create a reactive var to be used when the course is added
attachmentIdVar = new ReactiveVar(attachmentId);
},
'submit form': function (evt, temp) {
evt.preventDefault();
NewsEvents.insert({
title: $('#title').val(),
description: $('#description').val(),
type: $('input[name=netype]:checked').val(),
coverImageId: imageIdVar.get(),
attachmentId: attachmentIdVar.get(),
createdAt: new Date()
});
$('#title').val('');
$('#description').val('');
$("input:radio").removeAttr("checked");
console.log("done");
}
});
我想过使用if语句来检查,如果无功是truthy但这似乎累赘。
我使用下列程序包:
CFS:标准套餐
CFS:文件系统
无功无功
dburles:收集,助手
任何帮助高度赞赏。
尝试'console.log'在提交表单是否你的'imageIdVar'实际上是获得通过与否。 –
@KawsarAhmed:只要有人上传文件,它就会被传递。插入工作完美无瑕。但上传图片是可选的。所以如果用户没有上传图片,整个文档将不会被插入:( –