如何使用流星的简单模式插入用户用户名

How to insert users username using simple schema for meteor

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

这是我的代码:

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

没关系解决它这是我不得不放下的。希望对大家有帮助。

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

如果您并行使用 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