Inserting a momentjs object in Meteor Collection

前端 未结 3 1295
被撕碎了的回忆
被撕碎了的回忆 2021-01-11 18:16

I have a simple Meteor collection and I am trying to insert a document that has a momentjs property into it. So I do:

docId = Col.insert({m: moment()});
         


        
3条回答
  •  鱼传尺愫
    2021-01-11 19:15

    If you would like to have moment objects on find and findOne, save it as a date then transform it on finding it. For example:

    Posts = new Mongo.Collection('posts', {
      transform: function (doc) {
        Object.keys(doc).forEach(field => {
          if (doc[field] instanceof Date) {
            doc[field] = moment(doc[field]);
          }
        });
        return doc;
      }
    });
    
    Posts.insert({
      title: 'Hello',
      createdAt: new Date()
    });
    

提交回复
热议问题