Inside my libs folder I create collections using SimpleSchema. I want to add the Meteor.userId to some fields via autoValue like this:
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return Meteor.userId();
}
}
});
When doing this however, I receive the following error:
Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.
I tried this as well:
var userIdentification = Meteor.userId();
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return userIdentification;
}
}
});
This will crash my application though:
=> Exited with code: 8
=> Your application is crashing. Waiting for file change.
Any ideas?
userId
information is provided to autoValue
by collection2
through this
The autoValue option is provided by the SimpleSchema package and is documented there. Collection2 adds the following properties to this for any autoValue function that is called as part of a C2 database operation:
- isInsert: True if it's an insert operation
- isUpdate: True if it's an update operation
- isUpsert: True if it's an upsert operation (either upsert() or upsert: true)
- userId: The ID of the currently logged in user. (Always null for server-initiated actions.)
So your code should read as:
Collection = new Meteor.Collection('collection');
Collection.attachSchema(new SimpleSchema({
createdByUser: {
type: String,
max: 20,
autoValue: function() {
return this.userId;
}
}
});