如何使用简单模式为meteor插入用户用户名

时间:2015-10-12 14:26:00

标签: mongodb meteor simple-schema

我正在尝试插入登录用户的用户名,但每当我提交表单时。该集合永远不会被插入。只有在我注释掉createdBy字段时才会插入它。我目前正在使用包User Accounts作为我的登录/登录程序

这是我的代码:

createdBy: {
    type: String,
    autoValue: function() { 
        return this.field('username'); 
    }
},
createdAt: {
    type: Date,
    autoValue: function() {
        return new Date();
    }
}

2 个答案:

答案 0 :(得分:4)

没关系解决它,这是我必须放下的。希望它可以帮到某人。

createdBy: {
        type: String,
        autoValue: function() {
            return Meteor.user().username
        }
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        }
    }

答案 1 :(得分:2)

如果您并行使用collection2,则可以简单地执行以下操作以在服务器上进行相应的验证。

createdBy: {
  type: String,
  denyUpdate: true,      // for a createdBy, shouldn't be able to update.
  autoValue: function () {
    if (this.isInsert) {
      return this.userId;
    }
  },
  allowedValues: function () {
    return Meteor.users.find((doc) => doc._id);
  }
}

(来自autoValue documentation