javascript - Meteor: Insert field into MongoDB only if a value exists -


i have form coverimage , attachment optional. however, user has fill out forms. if not, meteor prints warning:

uncaught referenceerror: imageidvar not defined

i understand error message comes from.

so, how can make fields optional while inserting document collection?

my template helpers:

template.adminnewsevents.events({     'change #coverimage': function(evt, temp) {     /* fs.utility.eachfile(event, function(file) {       images.insert(file, function (err, fileobj) {         // inserted new doc id fileobj._id, , kicked off data upload using http         if (err) throw err;       });     }); */          var image = event.target.files[0];          // insert image database         // getting image id use in course object         var imageobject = images.insert(image);          // image id stored in image object         var imageid = imageobject._id          // create reactive var used when course 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 id fileobj._id, , kicked off data upload using http         if (err) throw err;       });     }); */          var attachment = event.target.files[0];          // insert image database         // getting image id use in course object         var attachmentobject = attachments.insert(attachment);          // image id stored in image object         var attachmentid = attachmentobject._id          // create reactive var used when course 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");     } }); 

i thought using if-statement check if var truthy seems cumbersome.

i'm using following packages:

  • cfs:standard-packages

  • cfs:filesystem

  • reactive-var

  • dburles:collection-helpers

any highly appreciated.

all have install underscorejs in meteor project. before adding database check this

_.isundefined(imageidvar); 

this returns boolean whether imageidvar , attachmentidvar has data or not. if false, you'll skip image field coverimageid , attachmentidvar in insert method. mongdb schemaless, won't have problem inserting without these fields.

a better approach

var temp ={}; temp.title = $('#title').val(); // , other variables if(!_.inundefined(imageidvar.get())) {     temp.coverimageid = imageidvar.get() } // you'll attachment id. you'll insert temp variable in insert method newsevents.insert(temp); 

Comments