1

カスタム ユーザー フィールドを Meteor ユーザー オブジェクト ( Meteor.user) に追加できません。ユーザーに「ステータス」フィールドを持たせたいのですが、それを「プロファイル」(つまり、profile.status) の下に入れ子にしたくはありません。(既に削除しましautopublishた。)

フィールドをクライアントに問題なく公開できました

Meteor.publish("directory", function () {
  return Meteor.users.find({}, {fields: {username: 1, status: 1}});
});

...しかし、ログインしたユーザーが自分の を更新できるようにする権限を設定できませんstatus

私が行った場合

Meteor.users.allow({
  update: function (userId) {     
    return true; 
}});

ユーザーは、すべてのユーザーのすべてModels.js,のフィールドを編集できます。それはクールではない。

私は次のようなバリアントを試しました

Meteor.users.allow({
  update: function (userId) {     
    return userId === Meteor.userId(); 
}});

Meteor.users.allow({
  update: function (userId) {     
    return userId === this.userId(); 
}});

コンソールに Access Denied エラーが表示されます。

ドキュメンテーションはこれにいくらか対処していますが、十分な詳細には触れていません。私が犯しているばかげた間違いは何ですか?

(これはこのSOの質問に似ていますが、その質問はフィールドの公開方法のみを扱い、更新方法は扱いません。)

4

2 に答える 2

5

これが私がそれを機能させる方法です。

サーバーでuserDataを公開します

Meteor.publish("userData", function () {
  return Meteor.users.find(
    {_id: this.userId},
    {fields: {'foo': 1, 'bar': 1}}
  );
});

次のように許可を設定します

Meteor.users.allow({
  update: function (userId, user, fields, modifier) {
    // can only change your own documents
    if(user._id === userId)
    {
      Meteor.users.update({_id: userId}, modifier);
      return true;
    }
    else return false;
  }
});

クライアントコードのどこかで、ユーザーがいる場合にのみユーザーレコードを更新します

if(Meteor.userId())
{
 Meteor.users.update({_id: Meteor.userId()},{$set:{foo: 'something', bar: 'other'}});
}
于 2013-05-30T06:19:11.107 に答える
3

試す:

Meteor.users.allow({
  update: function (userId, user) {     
    return userId === user._id; 
  }
});

collection.allow のドキュメントから:

update(userId, doc, fieldNames, 修飾子)

ユーザー userId がドキュメント doc を更新しようとしています。(doc は、提案された更新を含まない、データベースからのドキュメントの現在のバージョンです。) 変更を許可するには、true を返します。

于 2013-05-17T04:02:06.397 に答える