meteor-rest
meteor-rest copied to clipboard
this.userId is null in collection2 autoValue when calling method using POST request.
I'm using autoValue in one of collections (collection2), where i call this.userId.
When i use Meteor.call (or ddpclient.call in node.js client) all working perfectly.
But when i call this method using post request to http://localhost:3000/methods/
Is it normal behaviour and i need to set userId in method and check isFromTrustedCode in autoValue?
Did you pass the login token in the Authorization header? See more here: https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.md#authentication
Yes, i pass login token. Auth not working without token, i'm checking for this.userId in method
if (!this.userId) {
throw new Meteor.Error(403, "not authorized");
}
and without token i get 403 (as expected).
With token i get normal this.userId in method, but in autoValue (collection2/simpleSchema) this.userId is null. And this happens only when i call this method using POST request. If i call method using Meteor.call, this.userId in autoValue returns normal userId.
This is because this package uses a fake method invocation, so Meteor.userId() will not be correct even though this.userId is. I'm guessing this would not be too hard to fix, but I'm not sure how without digging into the core method code more.
Oh yeah this should be easy - we just need to use Meteor.bindEnvironment correctly, I think.
Hi I am very excited by this module, but I seem to be having a similar or related problem to the above....
I am using simple-rest 1.1.1 and simple-rest-accounts-password 1.1.2.
Calling methods through the simple-rest interface report the following: "Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions."
It turns out that this.userId is fine, but my methods (which are also being used by my meteor clients as well) are expecting Meteor.userId() and Meteor.user() during validation which don't seem to exist when I call the methods via simple-rest. I could change all my server methods to use this.userId, but I am also using some collection hook modules (that work for meteor clients, but not simple-rest) that also expect Meteor.userId() and Meteor.user().
Any suggestions?
@stubailo Any hope we can get this fixed soon? It doesn't look like it was included in the 1.0 release as reported by @aldeed . I may be able to provide a small bounty.
Found this workaround on SO in the answer from Anthony Castle
type: String, autoValue:function(){ if(Meteor.isClient){ return this.userId; }else if(Meteor.isServer){ return Meteor.userId(); } },