3

I'm trying to insert the logged in users username but whenever I submit the form. The collection never gets inserted. It only gets inserted if I comment out the createdBy field. I'm currently using the package User Accounts as my login/signin procedure

Here is my code:

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

2 Answers2

4

Never mind solved it here is what I had to put down. Hope it helps someone.

createdBy: {
        type: String,
        autoValue: function() {
            return Meteor.user().username
        }
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        }
    }
Mr.Bill
  • 179
  • 14
2

If you are using collection2 in parallel, you can simple do the following to validate it on the server appropriately.

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);
  }
}

(comes from the autoValue documentation)

corvid
  • 10,733
  • 11
  • 61
  • 130