我有一個表格,其中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:只要有人上傳文件,它就會被傳遞。插入工作完美無瑕。但上傳圖片是可選的。所以如果用戶沒有上傳圖片,整個文檔將不會被插入:( –