Ошибка трекера afterFlush: невозможно прочитать значение свойства из контекста данных в обратном вызове, отображаемом шаблоном

Я делаю простое приложение Meteor, которое может перенаправлять на страницу, когда пользователь щелкает ссылку. В шаблоне «перенаправление» я пытаюсь получить значение свойства «url» из экземпляра шаблона. Но я получаю правильное значение только при первом нажатии на ссылку. Когда я нажимаю F5, чтобы обновить страницу «перенаправление», я продолжаю получать это сообщение об ошибке:

Исключение из функции Tracker afterFlush: невозможно прочитать URL-адрес свойства null TypeError: невозможно прочитать URL-адрес свойства null в Template.redirect.rendered (http://localhost:3000/client/redirect.js?abbe5acdbab2c487f7aa42f0d68cf612f472683b:2:17) со значением null.

Вот на что указывает debug.js: (строка 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));
  }

Можете ли вы сказать мне, почему я не могу прочитать значение свойства «url» из контекста данных шаблона в обратном вызове, отображаемом шаблоном?

Это мой код (для получения более подробной информации вы можете посетить мой репозиторий):

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);
  }
});

перенаправление.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});
  }
});

публикация.js

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


person Nguyenducthanh    schedule 02.02.2015    source источник


Ответы (2)


Добавь это

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");
    }
  }
});

Скажи мне, если работает.

person Ethaan    schedule 02.02.2015
comment
Извините, но я снова получаю сообщение об ошибке при проверке во второй раз. - person Nguyenducthanh; 02.02.2015
comment
Я попытался убедиться, что все свежее, используя Crt + C и команду сброса метеора. Но я продолжаю получать одно и то же сообщение об ошибке при втором нажатии F5. - person Nguyenducthanh; 02.02.2015
comment
Большое спасибо за попытку помочь мне. Вот что я получаю после добавления вашего кода. imgur.com/LoTOSFe - person Nguyenducthanh; 02.02.2015

С помощью моего коллеги я могу решить проблему. Моя проблема связана с неправильным синтаксисом Meteor.subscribe. В моем коде я забыл «возврат» в функции waitOn. Это заставит Meteor не знать, когда данные будут полностью загружены. Вот правильный синтаксис:

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

person Nguyenducthanh    schedule 03.02.2015