Tracker afterFlush error : Cannot read value of a

2019-09-08 08:54发布

I'm making a simple Meteor app that can redirect to a page when user click a link. On 'redirect' template, I try get the value of property 'url' from the template instance. But I only get right value at the first time I click the link. When I press F5 to refresh 'redirect' page, I keep getting this error message:

Exception from Tracker afterFlush function: Cannot read property 'url' of null TypeError: Cannot read property 'url' of null at Template.redirect.rendered (http://localhost:3000/client/redirect.js?abbe5acdbab2c487f7aa42f0d68cf612f472683b:2:17) at null.

This is where debug.js points to: (line 2)

 if (allArgumentsOfTypeString)
      console.log.apply(console, [Array.prototype.join.call(arguments, " ")]);
    else
      console.log.apply(console, arguments);

  } else if (typeof Function.prototype.bind === "function") {
    // IE9
    var log = Function.prototype.bind.call(console.log, console);
    log.apply(console, arguments);
  } else {
    // IE8
    Function.prototype.call.call(console.log, console, Array.prototype.slice.call(arguments));
  }

Can you tell me why I can't read the value of 'url' property from template data context in template rendered callback?

This is my code (for more details, you can visit my repo):

HTML:

<template name="layout">
  {{>yield}}
</template>

<template name="home">
  <div id="input">
    <input type="text" id="url">
    <input type="text" id="description">
    <button id="add">Add</button>
  </div>
  <div id="output">
    {{#each urls}}
      {{>urlItem}}
    {{/each}}
  </div>
</template>

<template name="redirect">
  <h3>Redirecting to new...{{url}}</h3>
</template>

<template name="urlItem">
  <p><a href="{{pathFor 'redirect'}}">
    <strong>{{url}}: </strong>
  </a>{{des}}</p>
</template> 

home.js

Template.home.helpers({
  urls: function(){
    return UrlCollection.find();
  }
});
Template.home.events({
  'click #add': function() {
    var urlItem = {
      url: $('#url').val(),
      des: $('#description').val()
    };

    Meteor.call('urlInsert', urlItem);
  }
});

redirect.js

Template.redirect.rendered = function() {
  if ( this.data.url ) {
    console.log('New location: '+ this.data.url);
  } else {
    console.log('No where');
  }
}
Template.redirect.helpers({
  url: function() {
    return this.url;
  }
});

router.js

Router.configure({
  layoutTemplate: 'layout'
})
Router.route('/', {
  name: 'home',
  waitOn: function() {
    Meteor.subscribe('getUrl');
  }
});
Router.route('/redirect/:_id', {
  name: 'redirect',
  waitOn: function() {
    Meteor.subscribe('getUrl', this.params._id);
  },
  data: function() {
    return UrlCollection.findOne({_id: this.params._id});
  }
});

publication.js

Meteor.publish('getUrl', function(_id) {
  if ( _id ) {
    return UrlCollection.find({_id: _id});
  } else {
    return UrlCollection.find();
  }
});

2条回答
Emotional °昔
2楼-- · 2019-09-08 09:17

With help from my colleague, I can solve the problem. My problem comes from wrong Meteor.subscribe syntax. In my code, I forget "return" in waitOn function. This will make Meteor do not know when the data is fully loaded. Here is the right syntax:

waitOn: function() {
  return Meteor.subscribe('getUrl', this.params._id);
}

查看更多
老娘就宠你
3楼-- · 2019-09-08 09:18

Add this

Router.route('/redirect/:_id', {
  name: 'redirect',
  waitOn: function() {
    Meteor.subscribe('getUrl', this.params._id);
  },
  data: function() {
    if(this.ready()){
     return UrlCollection.findOne({_id: this.params._id});
     }else{
     console.log("Not yet");
    }
  }
});

Tell me if works.

查看更多
登录 后发表回答