0
votes

I have a simpleSchema:

  imageUrl: {
    type: Object,
    optional: true,
    autoValue: function() {
      if (Meteor.isClient) return;
      var imageField = this.field('imageId');
      if (!imageField.isSet){
        this.unset();
      } else {
        var imageObj = MealsImages.findOne(imageField.value);
        if (imageObj){
          return {thumb: imageObj.S3Url('thumb'), big: imageObj.S3Url('big')};
        }
      }
    },
    autoform: {
      label: false,
      type: 'hidden',
      afFieldInput: {
        type: "hidden"
      }
    }
  },

For some reason, when I update the record this field always appears in $unset array:

Meteor.methods({
  mealUpsert: function(doc, mealId) {
    check(doc, Meals.simpleSchema());

    console.log('test7');
    console.log(doc);

    if (mealId){
      Meals.update({_id: mealId}, doc);
    } else {
      mealId = Meals.insert(doc);
    }
    return false;
  }
});

Will print:

I20150830-21:49:39.560(-4)? { '$set': 
...
I20150830-21:49:39.562(-4)?   '$unset': 
I20150830-21:49:39.562(-4)?    { imageUrl: '',
...

I'm using autoform:

<template name="mealUpdateForm">
  <div class="meal-content">
    {{> quickForm collection="Meals" doc=this id="mealUpdateForm" meteormethod="mealUpsert" type="method-update"}}
  </div>
</template>

And will never be updated or set. Any clue why field could appear in $unset list?

1
You need to show the code which creates the doc that gets passed to your method. - Michel Floyd
I use autoform, the code is automatically created by it. So it may be the issue of autoform too. Will update description. - Kostanos

1 Answers

0
votes

I think I've figured this out - you have a this.unset() in your autoValue for imageUrl. This is called whenever you omit imageId from the modifier, even if it is already present in a document you are modifying!