2016-12-10 59 views
0

我發佈的用戶數據在服務器包括出生場的日期流星使用額外字段發佈

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

我想在服務器計算「時代」(例如年齡= 30),它本出版物中返回給客戶機。我怎樣才能實現它?謝謝!

回答

0

你可以做,通過提供transform選項給find方法:

Meteor.publish('sharedUserData', function(uid) { 
    return Meteor.users.find({ 
    _id: uid, 
    }, { 
    fields: { 
     dob: 1 
    }, 
    transform(doc) { 
     const { 
     dob, 
     } = doc; 

     doc.age = calculateAge(dob); 

     return doc; 
    }, 
    }); 
}); 
+0

謝謝!這似乎是我需要的,但問題是我不能運行這種轉換方法(我使用的代碼,但轉換沒有被稱爲發佈時) 從另一方面,我定義了_transform Meteor.users方法,它工作得很好。 –