在流星中向用户集合添加字段的正确方法 [英] Proper way to add field to the user collection in meteor

查看:72
本文介绍了在流星中向用户集合添加字段的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试将account_type字段添加到用户集合中。

So I'm trying to add an 'account_type' field to the user collection.

Meteor.startup(function () {
    if (Meteor.users.find().count() === 0){
        var user = Accounts.createUser({
            email: 'email@fake.com',
            password: 'password'
        });
        Meteor.users.update({_id: user}, {$set : {account_type: 'admin'}});
    }
});

当我打电话给 Meteor.user()。account_type ,它是未定义的。

When I call Meteor.user().account_type, it's undefined.

我也读过某些地方可能需要这样的事情:

I also read somewhere that something like this may be necessary:

Meteor.methods({
    get_user: function(user_id){
        return Meteor.users().find({ _id: user_id}, {fields: {account_type: 1}});
    }
});

但是当我打电话时我再次被定义:

But I get undefined again when I call it:

console.log(Meteor.call('get_user', Meteor.userId()));

添加到用户模型的正确方法是什么?

What is the proper way to add to the user model?

推荐答案

如果您希望在客户端上显示帐户类型,则需要创建包含所需字段的发布/订阅频道。 Meteor默认只发布用户名电子邮件个人资料。在99%的情况下,调用一个从db获取字段的方法是一个坏主意。

If you want to have account type visible on the client, you need to create publish / subscribe channel with needed field. Meteor only publishes username, email and profile by default. Calling a method to get a field from db is a bad idea in 99% of cases.

首先,服务器代码:

Meteor.publish('users', function() {
    return Meteor.users.find({}, {fields: {accountType: 1}});
});

客户:

Deps.autorun(function() {
    Meteor.subscribe('users');
});

当你开始运行时,接下来要确保客户端没有获得有关其他用户的敏感信息。

When you get this running, next make sure that client doesn't get sensitive information about other users.

这篇关于在流星中向用户集合添加字段的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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