在Meteor中添加收集物品作为路线

时间:2016-02-01 14:13:45

标签: meteor collections routes iron-router

我有一个流星项目,我的所有用户都使用路径以这种方式设置自己的个人资料页面:

路线代码:

Router.route('/@:username', {
  name: 'profile',
  controller: 'ProfileController'
});

ProfileController = RouteController.extend({
  template: 'profile',
  waitOn: function() {
    return Meteor.subscribe('userProfile', this.params.username);
  },
  data: function() {
    var username = Router.current().params.username;
    return Meteor.users.findOne({
      username: username
    });
  }
});

服务器代码:

Meteor.publish('users', function() {
  return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1, roles: 1}});
});

Meteor.publish('userProfile', function(username) {
  // Try to find the user by username
  var user = Meteor.users.findOne({
    username: username
  });
  // If we can't find it, mark the subscription as ready and quit
  if (!user) {
    this.ready();
    return;
  }
  // If the user we want to display the profile is the currently logged in user
  if(this.userId === user._id) {
    // Then we return the curresonding full document via a cursor
    return Meteor.users.find(this.userId);
  } else {

    return Meteor.users.find(user._id, {
      fields: {
        profile: 0
      }
    });
  }
});

我想用我设置的页面集合做类似的事情。创建集合作品和集合页面具有_id字段,该字段在创建时生成。

目前,该程序适用于mysite.com/@工作的用户。现在我想要为mysite.com/&

工作同样的事情

我基本上尝试使用用户名在上面的代码中执行完全相同的操作,但它无法正常工作。我已经检查过以确保我的收藏品的创建工作正常。但不知何故,我无法弄清楚如何使用集合做同样的事情,因为我对使用路线相对较新。

这就是我的尝试:

这是我的路线:

var pageRoute = '/&:_id';
Router.route(pageRoute, {
  name: 'page',
  controller: 'PageController'
});

PageController = RouteController.extend({
  template: 'page',
  waitOn: function() {
    return Meteor.subscribe('Page', this.params._id);
  },
  data: function() {
    var _id = Router.current().params._id;
    return Meteor.pages.findOne({
      _id: _id
    });
  }
});

服务器代码:

Meteor.publish('pages', function() {
  return Pages.find({});
});

Meteor.publish('Page', function(_id) {
  // Try find the page by _id
  var page = Meteor.pages.findOne({
    _id: _id
  });
  // If we can't find it, mark the subscription as ready and quit
  if (!page) {
    this.ready();
    return;
  }
  // If the page we want to display is not claimed, display it
  if(true) {
    return Meteor.pages.find(this._id);
  } else {
    // Redirect to the page
  }
});

页面集的架构:

_id :, createdAt :, 由...制作: , 声称: 声明:,

更新

我已经解决了这个问题,我在控制台服务器端遇到以下错误:

I20160202-11:16:24.644(2)? Exception from sub qrPage id 2kY6RKCTuCpBDbuzm TypeError: Cannot call method 'findOne' of undefined
I20160202-11:16:24.645(2)?     at [object Object].process.env.MAIL_URL [as _handler] (server/ecclesia.life_server.js:40:33)
I20160202-11:16:24.645(2)?     at maybeAuditArgumentChecks (livedata_server.js:1698:12)
I20160202-11:16:24.645(2)?     at [object Object]._.extend._runHandler (livedata_server.js:1023:17)
I20160202-11:16:24.645(2)?     at [object Object]._.extend._startSubscription (livedata_server.js:842:9)
I20160202-11:16:24.646(2)?     at [object Object]._.extend.protocol_handlers.sub (livedata_server.js:614:12)  
I20160202-11:16:24.646(2)?     at livedata_server.js:548:43                                                   

每当我尝试指向mysite.com/&<_id>

时就会发生此错误

2 个答案:

答案 0 :(得分:0)

基于此网站:https://perishablepress.com/stop-using-unsafe-characters-in-urls/

看起来#被认为是在URL字符串中使用的不安全字符。在上面的网页上,看起来您可以使用多个符号作为安全字符。

我刚在自己的机器上试过这个,当网址中引入了#时,我认为Meteor不会很好用。

答案 1 :(得分:0)

这让它有效......

出版物:

Meteor.publish('qrpages', function() {
  return QRPages.find({});
});

Meteor.publish('qrPage', function(id) {
  // Try find the qrpage by _id
  var qrpage = QRPages.find({_id: id});
  // If we can't find it, mark the subscription as ready and quit
  if (!qrpage) {
    this.ready();
    return;
  }
  return qrpage;
});

路线:

var qrpageRoute = '/$:_id';
Router.route(qrpageRoute, {
  name: 'qrpage',
  controller: 'QRController'
});

QRController = RouteController.extend({
  template: 'qrpage',
  waitOn: function() {
    var id = this.params._id;
    return Meteor.subscribe('qrPage', id);
  },
  data: function() {
    var id = this.params._id;
    return QRPages.findOne({
      _id: id
    });
  }
});
相关问题