如何显示此集合? [英] How do I display this collection?

查看:49
本文介绍了如何显示此集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作个人资料页面,我需要将个人资料名称和简介显示到模板中.问题是我无法获得每个配置文件对象的 Id.如果我可以像在带有 postId 的 book 中那样获得每个配置文件的 Id.这里 下面的代码是我认为可以工作的方式,但是没有.如果您能告诉我如何获取 ID,那就太感谢了.

I am trying to make profile page and, I need to display the profilename and bio to a template. The problem is I can't get the Id of each profile object. If I can get the Id of each profile like how it's done in the book with postId. Here The code below is the way I thought it would work but did not. If you tell me how to get the Id that would be great thanks.

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;
});

推荐答案

你似乎对一些 Meteor 的想法有点困惑.

You seem a little confused on some Meteor ideas.

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

Firstly Meteor.methods({ ... }) are functions that can be called client side using Meteor.call({ })

它们应该出现在顶层,而不是像 Accounts.onCreateUser 这样的其他函数内部

They should appear at the top level and not inside other functions like Accounts.onCreateUser

对于这个用例,我不明白为什么你需要一个方法.您要做的就是检索您将要存储在将要发送到客户端的数据中的数据.

For this use case I don't see why you need a method at all though. All you want to do is retrieve data that you will be storing in data that is going to be sent to the client anyway.

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

If you use the accounts-base package then you automatically get a Meteor.users collection that would be fine for what you want to do. I don't see the need for a Profile collection

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

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

Note I store the bio in the profile section of the user. This is because a user can edit their own profile section by default and so I can do Meteor.users.update( ... ) client side.

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

This is just an example to show some of the concepts. It does have bad practice in it. For a start I would recommend adding the package accounts-ui and using the {{> loginButtons}} helper. It gives you the error checking and so on. The reason I didn't use it was because I wanted to show how to allow a user to enter their bio prior to creating the account and how you can use that in Accounts.onCreateUser.

这篇关于如何显示此集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆