Strange behavior when using a function with autoform / simpleSchema to pull values for a select control

The following github project demos what I’m seeing:

And also this meteorpad:
http://meteorpad.com/pad/FXr4TYQdZbRDRZxLa/AutoForm%20Options%20issue

I have a field in the autoform that pulls options from the database, but it seems to be calling this function 4 times when displaying the form.

  'sequelTo': {
    type: String,
    label: "Sequel To",
    optional:true,
    autoform: {
      options: function () {
        var options = Books.find({}, {sort: {"title": 1}}).map(function (c) {
          return {label: c.title, value: c._id};
        });
        console.log("getting books for autoform options, found " + options.length);
        return options;
      }
    }
  }, 

and when viewing the form in the browser, I see this in the console:

getting books for autoform options, found 4

getting books for autoform options, found 4

getting books for autoform options, found 4

getting books for autoform options, found 4

(the number found depends on how many books are being pulled from the collection… point is, this message shows 4 times…and it seems like overkill to do a .Find on the collection 4 times in a row, getting the same results each time)

Is there something firing here that shouldn’t be??

Thanks for looking at this issue!

What I’m interested in is whether the outer function is reactive when Books.find({}) changes. No idea why it’ll be called 4 times though, but it’s too much of a coincidence that you’ve got 4 documents there… no?

  1. Yes, it is reactive… it’s really cool, you can be looking at a form, with a certain value selected in the drop down list, then change the book title in another instance, and the title changes in the drop down list.

  2. Yes, just a coincidence, it prints 4 times regardless of the number of books being populated into the select options.

1 Like

You probably don’t want to hear this, but I suspect it’s a case of subscriptions in iron:router being re-run multiple times. Have you considered using template subscriptions and changing over to flow-router?

Not a problem @robfallows, I have once switched to flow and then back, when another odd thing did not get better with Flow… but I read through this article again: https://meteorhacks.com/flow-router-and-subscription-management and it seems to say that even with flow router, I would probably do “route level” subscriptions instead of “template level” subscriptions, because I have routes that include parameters that are passed along to the subscription, like:

/filterThings/3

which would filter things of type 3, and then do a subscription to “things” with a parameter of 3, so, according to that article, you would want to use route level subs.

So, are you saying that route level subs might be the issue, or route level subs combined with Iron Router… or??

I tried keeping Iron Router and switching to template level subscriptions, here is the commit:

same issue though, it still displays the message 4 times… odd.

There is indeed something weird going on here. I removed all subscriptions from your iron:router routes and still see 4 runs of that options helper. Even replacing the find in that helper with a static array gets the same 4 runs!

Perhaps a bug in autoform and/or simple schema?

May be worth raising with @aldeed on github.

holy mackers! I’m getting it too, you’ve infected my code! I knew I shouldn’t have opened your meteorpad, damn youuuuuuuuuuuu.

This is what aldeed said of a similar issue, the creator didn’t specify 4, just multiple. i.e don’t do expensive stuff in your options function, just do cheap client side {label:value}

thank you for the comments!.. I did mention it over there https://github.com/aldeed/meteor-autoform/issues/1110