Meteor/MongoDB查看可用的发布字段

Meteor/MongoDB see available fields for publish?

本文关键字:布字段 字段 MongoDB Meteor      更新时间:2023-09-26

如何查看用户集合中哪些字段可供我从服务器发布和从客户端订阅?我正在使用Meteor.JS的Google Auth函数,但我使用它们来授权YouTube,因此字段不是标准的或记录的。

登录代码:

Meteor.loginWithGoogle({
    requestPermissions: ['profile', 'email', 'https://www.googleapis.com/auth/yt-analytics.readonly', 'https://www.googleapis.com/auth/youtube']
});

您可以检查服务器上Meteor.users中的记录,例如通过将它们记录到控制台。例如,在server.js中:

Meteor.startup(function() {
  Meteor.publish("nothing", function() { 
    if (this.userId)
      console.log(Meteor.users.findOne({_id: this.userId}));
  });
});

然后在客户端订阅:

Meteor.subscribe("nothing");

这将把登录用户的内容记录到服务器控制台(终端窗口)。它位于发布方法中的原因是Meteor不允许在方法之外访问当前用户,因此我将其命名为"nothing",以表明它不做任何事情,仅用于临时检查目的。