簡體   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