2014-10-30 21 views
1

當我查詢Meteor.users時,我沒有收到services字段或我在profile以外創建的任何其他自定義字段。爲什麼我只在客戶端上收到_idprofile以及如何收到整個Meteor.users對象?訪問Meteor.users的服務字段

謝謝。

回答

3

從文檔

默認情況下,當前用戶的用戶名,電子郵件和個人資料發佈到客戶端。您可以發佈更多的領域與當前用戶:

就像上面說如果你想你需要發佈他們

// server 

Meteor.publish("userData", function() { 
    if (this.userId) { 
    return Meteor.users.find({_id: this.userId}, 
          {fields: {'services': 1, 'others': 1}}); 
    } else { 
    this.ready(); 
    } 
}); 

//客戶

Meteor.subscribe("userData"); 
0

以上回答不工作等領域,但這意味着您必須訂閱所述數據,如果您從當前登錄的用戶以外獲取數據,則應該執行此操作。

但是,如果您關心的只是登錄用戶的數據,那麼您可以改爲使用null發佈來獲取數據,而無需訂閱。

在服務器上執行,

Meteor.publish(null, function() { 
    if (! this.userId) { 
    return null; 
    } 

    return Meteor.users.find(this.userId, { 
    fields: { 
     services: 1, 
     profile: 1, 
     roles: 1, 
     username: 1, 
    }, 
    }); 
}); 

這實際上是賬戶包做什麼under the hood