Meteor submiting form: Uncaught TypeError: Cannot read property'vk' of undefined

Meteor submiting form: Uncaught TypeError: Cannot read property'vk' of undefined

我正在使用 accounts-vk 通过 vkontakte.ru 提供登录 在我删除 meteor-autopublish 包之前,一切都完美无缺。 我的 connectSubmit.js:

    Template.connectSubmit.events({
        'submit form': function(e) {
            e.preventDefault();
            var query = {
                vk_id: Meteor.user().services.vk.id,
                user_id: Meteor.userId(),
                photo: Meteor.user().services.vk.photo          
            };
        query._id = Connects.insert(query);
        Router.go('index');
    }
  });

错误是:

Uncaught TypeError: Cannot read property 'vk' of undefined

还有我的模板的第二个问题: 这在我删除自动发布之前效果很好:

{{currentUser.services.vk.first_name}} 

但是现在不工作了。 我认为 Meteor.publish 功能有问题,但我不知道如何解决它。

autopublish 开启时,它将发布 所有 您用户的字段。当您删除 autopublish 时,只会发布几个字段(用户名、_id、电子邮件、个人资料)。

如果您发布 services 字段,您将向客户端公开登录令牌和散列密码等内容。显然,您永远不想在生产中这样做(这就是为什么 autopublish 应该始终被删除的原因)。有关详细信息,请参阅我的 common mistakes 文章的 "published secrets" 部分。

所以对你的问题的简短回答是,客户一开始就不应该做任何这些事情。

长话短说,如果您有需要这些数据的逻辑,则需要做以下两件事之一(这可能需要单独的问题):

如果服务信息仅用于数据突变

在这种情况下使用服务器端方法。在您的 submit 事件中,您将调用一个方法,该方法对当前用户执行 findOne 并更新 Connects 集合。在这里读取 services 数据是安全的,因为代码是 运行 在服务器上。

如果客户端需要服务信息(例如照片)

您需要将数据从 services 复制到 profile 等安全字段中。您可以在用户 creates her account or whenever she logs in.

时执行此操作