當我查詢Meteor.users
時,我沒有收到services
字段或我在profile
以外創建的任何其他自定義字段。爲什麼我只在客戶端上收到_id
和profile
以及如何收到整個Meteor.users
對象?訪問Meteor.users的服務字段
謝謝。
當我查詢Meteor.users
時,我沒有收到services
字段或我在profile
以外創建的任何其他自定義字段。爲什麼我只在客戶端上收到_id
和profile
以及如何收到整個Meteor.users
對象?訪問Meteor.users的服務字段
謝謝。
從文檔
默認情況下,當前用戶的用戶名,電子郵件和個人資料發佈到客戶端。您可以發佈更多的領域與當前用戶:
就像上面說如果你想你需要發佈他們
// 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");
以上回答不工作等領域,但這意味着您必須訂閱所述數據,如果您從當前登錄的用戶以外獲取數據,則應該執行此操作。
但是,如果您關心的只是登錄用戶的數據,那麼您可以改爲使用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