meteor-rest icon indicating copy to clipboard operation
meteor-rest copied to clipboard

this.userId is null in collection2 autoValue when calling method using POST request.

Open scsirdx opened this issue 10 years ago • 7 comments
trafficstars

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/, i get error: 400, reason: 'User is required'. And this.userId in autoValue is null.

Is it normal behaviour and i need to set userId in method and check isFromTrustedCode in autoValue?

scsirdx avatar Jul 18 '15 00:07 scsirdx

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

stubailo avatar Jul 18 '15 01:07 stubailo

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.

scsirdx avatar Jul 18 '15 08:07 scsirdx

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.

aldeed avatar Nov 19 '15 00:11 aldeed

Oh yeah this should be easy - we just need to use Meteor.bindEnvironment correctly, I think.

stubailo avatar Nov 19 '15 00:11 stubailo

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?

dpatte avatar Jul 15 '16 05:07 dpatte

@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.

dpatte avatar Jul 18 '16 19:07 dpatte

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

discdiver avatar Sep 05 '16 15:09 discdiver