0

I stuck with strange behaviour in meteor -> iron router

this.route('addItem', {
        path: '/additem/:_owner',
        action: function(){
            var c = Presentation.find().count();
            var id = Presentation.insert({
                owner: this.params._owner,
                order: c
            });
            this.redirect('editItem', {_id: id});
        }

this.route('editItem', {
        path: '/edititem/:_id',
        data: function(){return Presentation.findOne(this.params._id)}
    });

in this case, data is empty, but if c is const (var c = 1), all works correctly:

this.route('addItem', {
        path: '/additem/:_owner',
        action: function(){
            var c = 1;
            var id = Presentation.insert({
                owner: this.params._owner,
                order: c
            });
            this.redirect('editItem', {_id: id});
        }

this.route('editItem', {
        path: '/edititem/:_id',
        data: function(){return Presentation.findOne(this.params._id)}
    });
vnm
  • 11
  • 3

2 Answers2

0

Presentation is a collection on the client. It could have nothing in it, that Collection will get synched with whatever the server is setup to publish at some point, it will reactively update things that depend on it when synched, however in a route you are just assuming its synched, which is highly unlikely. You can put a waiton in your route. Or use a Meteor method, or do a server side route.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
0

solved with Meteor metod, but not understood (

next code shows in console rigth vsriables, but data empty.

this.route('addItem', {
        path: '/additem/:_owner',
        action: function(){
            var c = Presentation.find().count();
            console.log('c: '+c);
            var id = Presentation.insert({
                owner: this.params._owner,
                order: c
            });
            this.redirect('editItem', {_id: id});
        }

this.route('editItem', {
        path: '/edititem/:_id',
        data: function(){console.log('doc: ' + Presentation.findOne(this.params._id)); 
                         return Presentation.findOne(this.params._id)}
    });
vnm
  • 11
  • 3