Search code examples
meteoriron-router

meteor iron-router with autopublish not getting prefilled fields in update form


probably that has nothing to do with autoform but since I am not entirely sure, I thought I'd put this information here, too. In my form I do not get any prefilled fields which I would expect of an update form and also there is some strange output in my console that I do not understand.

The corresponding router part is:

// app/lib/routes.js
Router.route('/assignment/:_id', function(){
    var assignment = Assignments.findOne({_id: this.params._id});
    console.log('ass w/' + this.params._id);
    console.log(assignment);
    this.render('Assignment', {data: assignment});  
}, {name: 'assignment.show',
    waitOn: function(){
        console.log('do the wait');
        Meteor.subscribe('assignments');
    }
  }
);

As you see, there is a lot of debug output in there already. The route seems to be called correctly because the template is displayed after click.

Publishing is made here:

// app/server/publish.js
Meteor.publish('assignments', function() {return Assignments.find();})

Now, this code seems to be called multiple times. The overall output after getting into this route is:

do the wait
routes.js:22 ass w/M2gtLf9vbbWCTgxze
routes.js:23 undefined
debug.js:41 insert failed: Access denied. No allow validators set on restricted collection for method 'insert'.
routes.js:27 do the wait
routes.js:22 ass w/M2gtLf9vbbWCTgxze
routes.js:23 Object {_id: "M2gtLf9vbbWCTgxze", title: "Neuer Titel", priority: "high", description: "just do it"}

(The insert error probably comes from some other place) There might be some other bugs in there, too, but I hope the output shows the problem. What I do not get is that I seem to not get the assignment at first but in the second call it is received, which is strange to me (why does this happen?). I am not so sure about folder structures, but isn't publish.js called after route.js, so that there might something unpublished? If yes, where to put my files then?

Probably not important but this is my assignment.html

<template name="Assignment">
    <div class="panel panel-default" id="main">
        <div class="panel-heading">Change Assignment</div>
        <div class="panel-body">
            {{>quickForm collection="Assignments" id="updateAssignmentForm" type="method" meteormethod="updateAssignment"}}
        </div>
    </div>
</template>

So, why is the router called twice, why is there no output at first and why do I not get any prefilled fields here? (all fields are just empty in my form)


Solution

  • Your route function is running before the subscription has returned data. You can simply put this in a data function and it will wait:

    Router.route('/assignment/:_id',{
      data: function(){
        return Assignments.findOne({_id: this.params._id});  
      },
      name: 'Assignment',
      waitOn: function(){
        return Meteor.subscribe('assignments');
      }
    });