5

私は Meteor と Iron Router を使用して Web アプリを構築しています。私の目標の 1 つは、ログインしているユーザーのプロファイル ビュー (ユーザーが自分の情報を編集できる場所) と、すべてのユーザーのプロファイル ビュー (表示できるもの) を作成することです。任意の人によって)。

ログインしているユーザーのプロファイル ビューは正常に機能しますが、他のユーザーのユーザー プロファイル ビューの作成に問題があります。ブラウザーで url ( ) を使用してユーザー プロファイルに直接アクセスしようとすると、eg: localhost:3000/users/"id"ログインしているユーザーのデータがレンダリングされ、ブラウザーの URL が localhost: に変更されます3000/users/[object%20Object]

また、 への参照を含むタグは、その情報を含むページがレンダリングされるときは常に空です。

この問題に関連するコードは次のとおりです。

サーバー -publications.js

Meteor.publish('singleUser', function(userId) {
   return Meteor.users.find(userId);
});

router.js

Router.map(function() { 
    this.route('profile', {
        path:'/profile',
        data: function() {return Meteor.user();}
    });

    this.route('user_profile', {
        path: '/users/:_id',
        waitOn: function() {
                return Meteor.subscribe('singleUser', this.params._id);
        },
        data: function() { 
                var findById = Meteor.users.findOne(this.params._id);
                if (typeof findById !== "undefined") {
             Router.go(getProfileUrlById(findById),  {replaceState: true});
                }
        }
    }); 
});

ユーザー プロファイル テンプレート

<template name="user_profile">
    <h4>Username</h4>
    <p>{{username}}</p>
    <h4>Email:</h4>
    <p>{{email}}</p>    
</template> 

ユーザー プロファイル ヘルパー

Template.user_profile.helpers({
     username: function() {return Meteor.user().username},
     email: function() {return Meteor.user().emails[0].address} 
});

アイテム テンプレート

<template name="item">
    </span> <a href="{{profileUrl}}">{{author}}</a>
</template> 

アイテム ヘルパー

Template.item.helpers({
    profileUrl: function() {
        var user = Meteor.users.findOne(this.userId, {reactive:false});
        if(user)
            return getProfileUrlById(user);
    }
 });

getProfileUrlById = function(id) {
    return Meteor.absoluteUrl()+'users/' + id;
}

ユーザー プロファイル ログイン テンプレート

<template name="profile">
    <h4>Username</h4>
    <p>{{username}}</p>
    <h4>Email:</h4>
    <p>{{email}}</p>
</template>

ユーザー プロファイル ログイン ヘルパー

Template.profile.helpers({
    username: function() {return Meteor.user().username},
    email: function() {return Meteor.user().emails[0].address}
 });

何か不足していますか?

前もって感謝します!

4

3 に答える 3

1

findById取得しているの型はMeteor.users.findOneオブジェクトであるため、 で文字列にキャストすると、最終的にgetProfileUrlByIdになります[object Object]userIdおそらく、オブジェクト自体ではなく、オブジェクトからその関数に値を渡したいでしょう。

于 2013-12-18T20:41:49.067 に答える
1

主な問題は、プライベート データを他のユーザーのユーザー コレクションに公開していないことのようです (autopublishパッケージをアンインストールした場合)。以下のフィールドのみprofileが Meteor によってデフォルトで公開されます。usernameたとえば、フィールドが含まれprofileていないため、現在ログインしているユーザー以外のすべてのユーザーに対して公開されることはありません。

第二に、ルート生成が複雑になりすぎています。次のようにルートを生成してみてください。

{{#each users }}
<a href="{{ pathFor 'userAccount' }}">My account</a>
{{/each }}

_idこれにより、router.js ファイルが次のようになっていると仮定すると、フィールドがキーとして自動的に使用されます。

Router.map(function () {
  this.route('userAccount', {
    path: '/users/:_id'
  });
});
于 2014-01-24T07:32:38.400 に答える
0

ルートのデータ関数がuser_profileデータを返していません。つまり、戻り値がありません。そのはず:

data: function() { 
        return Meteor.users.findOne(this.params._id);
      }

これで何を達成したいのかわかりません(削除することをお勧めします)...

if (typeof findById !== "undefined") {
     Router.go(getProfileUrlById(findById),  {replaceState: true});
}

ルートが使用される場合、URL はすでにユーザーのプロファイル ID (/users/{id}) になっている必要があります。getProfileUrlByIdそれに加えて、関数はユーザーIDを文字列として期待しているのに対し、ユーザーオブジェクトで呼び出しています。

于 2014-08-27T17:27:17.400 に答える