2016-01-24 68 views
1

我有一個表格,其中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:收集,助手

任何幫助高度讚賞。

+0

嘗試'console.log'在提交表單是否你的'imageIdVar'實際上是獲得通過與否。 –

+0

@KawsarAhmed:只要有人上傳文件,它就會被傳遞。插入工作完美無瑕。但上傳圖片是可選的。所以如果用戶沒有上傳圖片,整個文檔將不會被插入:( –

回答

2

您所要做的就是在您的流星項目中安裝underscoreJS。然後添加到數據庫中檢查這樣

_.isUndefined(imageIdVar); 

在此之前返回布爾是否您imageIdVarattachmentIdVar有一些數據或沒有。因此,如果您輸入錯誤,您將跳過insert方法中的圖像字段coverImageIdattachmentIdVar。由於MongDB是無模式的,所以如果沒有這些字段,就不會出現問題。

一個更好的辦法

var temp ={}; 
temp.title = $('#title').val(); 
// and for other variables also 
if(!_.inUndefined(imageIdVar.get())) { 
    temp.coverImageId = imageIdVar.get() 
} 
// you'll do also for attachment ID. then you'll insert the temp variable in the insert method 
NewsEvents.insert(temp); 
+0

因此,這意味着,我必須構建4個不同的場景?如果兩個都定義了,我按照我的問題所示進行操作。另外三個插入函數:如果兩個都沒有定義(1),或者兩個中只有一個被定義爲(2)+(3).Hm ...沒有任何「$存在」用於插入新條目? –

+1

是的,你可以這樣做,或者考慮一個更好的解決方案,'$ exists'操作符用於你的db查詢,而不是插入值 –

+0

我找不到更好的解決方案。 。我知道''exists'''只是查詢語句,這就是爲什麼我想知道是否有'''$ exists''這樣的值來插入值。 –

0

感謝@Faysal艾哈邁德,我想出了一個解決方案。你必須設置ReactiveVar假開頭:

imageIdVar = new ReactiveVar(false); 
attachmentIdVar = new ReactiveVar(false); 

Template.adminNewsEvents.events({ 
    'change #coverImage': function(evt, temp) { 
     var image = event.target.files[0]; 
     var imageObject = Images.insert(image); 
     var imageId = imageObject._id 

     if (imageId) { 
      imageIdVar = new ReactiveVar(imageId); 
     } 
    }, 
    'change #attachment': function(evt, temp) { 
     var attachment = event.target.files[0]; 
     var attachmentObject = Attachments.insert(attachment); 
     var attachmentId = attachmentObject._id 

     if (attachmentId) { 
      attachmentIdVar = new ReactiveVar(attachmentId); 
     } 
    }, 
    'submit form': function (evt, temp) { 
     evt.preventDefault(); 

     var temp = {}; 
     temp.title = $('#title').val(); 
     temp.description = $('#description').val(); 
     temp.type = $('input[name=netype]:checked').val(); 
     temp.createdAt = new Date(); 

     if (imageIdVar.get()) { 
      temp.coverImageId = imageIdVar.get(); 
     } 

     if (attachmentIdVar.get()) { 
      temp.attachmentId = attachmentIdVar.get(); 
     } 

     NewsEvents.insert(temp); 
    } 
}); 
相關問題