Meteor:仅当存在值时才将字段插入MongoDB

时间:2016-01-24 16:45:52

标签: javascript mongodb validation meteor file-upload

我有一个表单,其中coverImage和附件是可选的。但是,目前用户必须填写所有表格。如果没有,流星打印警告:

  

未捕获的ReferenceError:未定义imageIdVar

我了解此错误消息的来源。

那么,如何在将文档插入集合时将字段设为可选字段?

我的模板助手:

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语句来检查var是否真实,但这看起来很麻烦。

我使用以下套餐:

  • CFS:标准封装

  • CFS:文件系统

  • 反应性-VAR

  • dburles:收集辅助细胞

任何帮助高度赞赏。

2 个答案:

答案 0 :(得分:2)

您只需在流星项目中安装underscoreJS即可。然后在添加到数据库之前检查这样

_.isUndefined(imageIdVar);

这会返回布尔值,而imageIdVarattachmentIdVar是否包含某些数据。因此,如果您出现错误,则会在coverImageId方法中跳过图片字段attachmentIdVarinsert。由于MongDB是Schemaless,因此如果没有这些字段,您将无法插入问题。

更好的方法

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

答案 1 :(得分:0)

感谢@Faysal Ahmed,我想出了一个解决方案。您必须在开头将 ReactiveVar设置为false

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