繁体   English   中英

使用Iron Router(METEOR)路由到特定的用户配置文件

[英]Routing to a specific user profile using iron router (METEOR)

正如问题所言,我需要为我拥有的每个用户创建一条特定的路由。 就我而言,雇主。 现在,网络上的所有示例都与USERS集合相关联。

在我的情况下,我想路由到: "/employer/:_id"但是我在Collection EmployersEmployer ID 所以基本上我必须通过User ID的密钥来获得Employer ID User ID

我有点坚持将雇主ID值返回到路线...

METHODS.js

getEmployerId: function(currentuser){
    employerId = Employer.find({"user":currentuser}).fetch();
    return employerId;
}

ROUTER.js

  Router.route("/employer/:_id", {
  name:"employer",
  template:"employer",
  layoutTemplate:'employerLayout',
    data: function(){
      var currentuser = Meteor.userId();
      Meteor.call("getEmployerId", currentuser, function(error, result){
        if(error){
          console.log("error", error);
        }
        if(result){
           return true;  // I belive here is where I have to pass it up to the ROUTE
        }
      });

    },
    onBeforeAction:function(){

      var user = Meteor.userId();
        if(!user || !Roles.userIsInRole(user, ['employer'])) {
          Router.go("verification");
        }else {
          this.next();
        }
        return true;

    },

});

这是我的雇主收藏集的样子:

meteor:PRIMARY> db.employer.find().pretty()
{
    "_id" : "qCFGZa4ogc5LR56PL", // I need this for the route
    "createdAt" : ISODate("2015-07-18T13:19:16.098Z"),
    "user" : "owfJ4ozrfsp26o8G4" // the key through which i can return the ID, got it from the user session?
}

有人建议如何做吗? 这是每个用户(雇主)​​资料的好方法吗? 任何教程,示例或任何使用用户个人资料描述应用程序的内容都将非常有用!

好的,看来您已经快到了。

我认为您不需要:: id参数。 您可以将用户简单地发送到/employer ,他在该位置进行登录,因此您拥有其用户ID。

然后将getEmployerId更改为getEmployer :换句话说,获取整个雇主记录。

getEmployer: function(currentuser){
    return Employer.find({"user":currentuser}).fetch();
}

然后在路由器的data:函数中,而不是返回true而是返回找到的记录。 这样,记录可用于您的模板(这就是data功能的作用)

data: function(){
  var currentuser = Meteor.userId();
  Meteor.call("getEmployer", currentuser, function(error, result){
    if(error){
      console.log("error", error);
    }
    if(result){
       return result;
    }
  });

},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM