【问题标题】:How do I display this collection?我如何显示这个集合?
【发布时间】:2014-08-09 05:46:35
【问题描述】:

我正在尝试制作个人资料页面,我需要在模板中显示个人资料名称和简历。问题是我无法获取每个配置文件对象的 ID。如果我可以像在book 中使用 postId 一样获取每个配置文件的 ID。 Here 下面的代码是我认为它可以工作但没有的方式。如果您告诉我如何获得 ID,那将非常感谢。

Profile = new Meteor.Collection('profile');

Profile.allow({
    update: ownsDocument
})

Profile.deny({
  update: function(profileId, profile, fieldNames) {
    return (_.without(fieldNames, 'bio').length > 0);
  }
});


Accounts.onCreateUser(function(options, user){
    Meteor.methods({
        profile: function(postAttributes) {
    var user = Meteor.user();
    var profile = _.extend(_.pick(options.profile, 'bio'), {
        userId: user._id, 
        profilename: user.username, 
        submitted: new Date().getTime(),
        postsCount: 0, posts : []
    });


    var profileId = Profile.insert(profile);

    return profileId;
    }

   });
    return user;
});

【问题讨论】:

  • 您可以在您的源代码示例中添加一些缩进吗?没有它就很难读。另外,为什么要在onCreateUser 回调中定义post 方法?我认为应该在全球范围内进行。
  • 好的,谢谢,我会尝试并立即修复我的代码中配置文件的错字,我一定是输入错误了
  • 创建新帐户时,我需要为每个用户创建个人资料。
  • 明白,但这不是 Meteor.methods 的目的。
  • 好吧,这只是我尝试过的其中一种方法,大多数没有包括 Meteor.methods 任何帮助都很好,我对流星很陌生。从理论上讲,您将如何创建它。谢谢

标签: javascript meteor user-profile


【解决方案1】:

在发现流星示例中,他们使用一种方法插入Post,然后返回其ID。在您的情况下,一个新的 Profile 被插入到异步回调中,因此您无法返回 id。但是,您知道userId,因此您可以使用它来获取个人资料。

服务器

Accounts.onCreateUser(function(options, user){

    var user = Meteor.user();
    var profile = _.extend(_.pick(options.profile, 'bio'), {
        userId: user._id, 
        profilename: user.username, 
        submitted: new Date().getTime(),
        postsCount: 0,
        posts : []
    });

    Profile.insert(profile);

    return user;
});

客户

Template.profile.profile = function () {
  return Profiles.findOne({userId: Meteor.userId()});
};

【讨论】:

    【解决方案2】:

    您似乎对 Meteor 的一些想法有些困惑。

    首先 Meteor.methods({ ... }) 是可以在客户端使用 Meteor.call({ }) 调用的函数

    它们应该出现在顶层,而不是在 Accounts.onCreateUser 等其他函数中

    对于这个用例,我不明白为什么你需要一个方法。您要做的就是检索您将存储在无论如何都会发送给客户端的数据中的数据。

    如果您使用 accounts-base 包,那么您会自动获得一个 Meteor.users 集合,该集合可以满足您的需求。我认为不需要个人资料集合

    这是一个流星垫说明它:http://meteorpad.com/pad/xL9C8eMpcwGs553YD

    注意我将简历存储在用户的个人资料部分。这是因为默认情况下用户可以编辑自己的个人资料部分,因此我可以在客户端执行 Meteor.users.update( ... )。

    这只是展示一些概念的示例。它确实有不好的做法。首先,我建议添加包 accounts-ui 并使用 {{> loginButtons}} 助手。它为您提供错误检查等。我没有使用它的原因是因为我想展示如何允许用户在创建帐户之前输入他们的简历,以及如何在 Accounts.onCreateUser 中使用它。

    【讨论】:

    • 感谢您的帮助
    • 唯一得不到的是this
    猜你喜欢
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多