Hi there, I’m running into an issue where when I call a meteor method from the shell, simple-schema
is throwing an error as it’s trying to detect Meteor.userId()
for my timestamp fields. I’ve tried testing for Meteor.userId
being available and falling back to "0"
but simple schema still throws an error.
I’ve reverted to commenting out that specific line when calling a method from the shell but it’s a bit frustrating! Are there any known work-arounds? Or ways for Simple Schema only to look for the user if available?
Thanks!
You might be able to use setUserId()
to set a userId
for your method calls from the shell.
Maybe create a method just for setting the userId
, then subsequent method calls would use that userId
?
Let me know if you come up with something that works.
Can you give some more context here? Are you using simple-schema alone, or with collection2? What do the definitions for your timestamp fields look like?
Thanks both and apologies for the slow reply.
I am indeed using simple-schema alongside collection2. To avoid having to write my 4 stamps on each collection definition, I’m using my own helper function which add my stamps to the schema I add to it (using Object.assign
, don’t think this should be an issue). Other than that my stamp fields look like:
createdAt =
type: Date
autoValue: ->
return new Date() if @isInsert
updatedAt =
type: Date
optional: true
autoValue: ->
return new Date() if @isUpdate
createdBy =
type: String
optional: true
autoValue: ->
return Meteor.userId() if @isInsert
updatedBy =
type: String
optional: true
autoValue: ->
return Meteor.userId() if @isUpdate
Thanks Bruce will also give setUserId a go!
Unfortunately when running from there won’t be a user since there’s no connection context. Methods have a connection context and so if you are using a method you could possibly use setUserId as @brucejo suggested. Since the value seems to be optional though, you could use the userId
property of the current context created by collection2 instead. Also you could use the isFromTrustedCode
property to allow the value to be set from server side code but only auto populated from the client.
{
createdBy: {
type: String,
optional: true,
regEx: SimpleSchema.RegEx.Id,
autoValue: function () {
if(!this.isSet || !this.isFromTrustedCode){
return this.userId
}
},
},
}
1 Like
Thanks! I think for my purposes it’s fine for the value to be not set or “0” or an equivalent fallback. I was mainly trying to write the autoValue part in such a way that it would set the current userId if it was accessible (and fallback if not)
1 Like