2015-02-09 72 views
0

我需要更改出版物中的日期格式,並且想知道它是否可用於Meteor。我非常喜歡這樣做:更改流星出版物中的日期格式

Meteor.publish('foo', function() { 
    var user = Meteor.users.findOne({_id: this.userId}); 
    if (user) { 
     var f = Foos.find({organizationId: user.profile.organizationId}) 
     return f.map(function(cx) { cx.createdAt = moment.utc(cx.createdAt).format(); return cx }); 
    } 

    return []; 
}); 

但如果我這樣做,我得到以下異常:

Exception from sub 2 Error: Publish function returned an array of non-Cursors 

回答

0

你最好不要讓出版本身發送的日期和改變文件格式在客戶端。

查看Mongo集合或集合的transform選項。 find - http://docs.meteor.com/#/full/find

Foos.find(
    {...}, 
    { 
    transform: function (doc) { 
     doc.createdAt = moment.utc(doc.createdAt).format(); 
     return doc; 
    } 
    } 
); 
+0

你認爲在客戶端上會搞亂小蒙戈? – 2015-02-10 04:42:21

+0

不,完全沒有。 Minimongo不知道日期的性質。例如,您仍應該能夠排序。 – 2015-02-10 05:33:46