繁体   English   中英

流星:仅在存在值的情况下将字段插入MongoDB

[英]Meteor: Insert field into MongoDB only if a value exists

我有一个Coverage和附件是可选的形式。 但是,当前用户必须填写所有表格。 如果不是,则流星打印警告:

未捕获的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:standard-packages

  • cfs:文件系统

  • 反应性变量

  • dburles:收集助手

任何帮助,高度赞赏。

您要做的就是在流星项目中安装underscoreJS 然后在添加到数据库之前像这样检查

_.isUndefined(imageIdVar);

这将返回boolean,无论您的imageIdVarattachmentIdVar是否包含某些数据。 因此,如果您得到的是假, coverImageIdinsert方法中跳过图像字段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);

感谢@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);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM