陨石铁路由器无法打印出属性

meteor iron router not able to print out property

本文关键字:打印 属性 石铁 路由器      更新时间:2023-09-26

所以我有路由器

Router.route('/', function () {
  console.log("my message");
  console.log(this.request.query.q);
  this.render('map');
});

对于http://localhost:3000/?q=somequery的网址

当我删除console.log(this.request.query.q)时,它工作正常,但添加此行会产生以下错误:

Exception in callback of async function: TypeError: Cannot read property 'q' of undefined

为什么流星找不到这个参数?

来自文档

http://eventedmind.github.io/iron-router/

如果 url 中有查询字符串或哈希片段,则可以使用 this.params 对象的查询和哈希属性访问它们。

// given the url: "/post/5?q=s#hashFrag"
Router.route('/post/:_id', function () {
  var id = this.params._id;
  var query = this.params.query;
  // query.q -> "s"
  var hash = this.params.hash; // "hashFrag"
});

尝试

this.params.q