【问题标题】:How to insert users username using simple schema for meteor如何使用流星的简单模式插入用户用户名
【发布时间】:2015-10-12 14:25:59
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: mongodb meteor simple-schema


    【解决方案1】:

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

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

    【讨论】:

    • 感谢发帖。帮了我:)
    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多