【问题标题】:Restivus authentication for Meteor methodsMeteor 方法的 Restivus 身份验证
【发布时间】:2016-10-11 04:28:24
【问题描述】:

我正在尝试为 REST 服务提供我的流星应用程序。为此,我使用了“Restivus”包,它也可以正常工作。但是一旦我想运行一个流星方法this.userId 是未定义的。

Api.addRoute('addArticle', {authRequired: true}, {
        post: function () {
            console.log(this.userId); //<-- hwuqtFXf8aKperJ5p
            try {
                Meteor.call("addArticle",this.bodyParams);
            } catch (e) {
                return {code:500,type:e.error,reason:e.reason};
            }
        }
    });

方法:

new ValidatedMethod({
    name: 'addArticle',
....
if (!this.userId) {
        throw new Meteor.Error(...); //is thrown
    }

我做错了什么?

【问题讨论】:

    标签: meteor restivus meteor-restivus


    【解决方案1】:

    在 Meteor 方法中,您通过执行获取当前 userId

        Meteor.userId() 
    

    而不是

        this.userId
    

    因此您需要将代码更新为

        if(!Meteor.userId()){ 
            throw new Meteor.Error(403, '403:Forbidden', 'You shall not pass!')
        }
    

    【讨论】:

    • 也许这曾经有效,但对我也不起作用,我可以在路由的回调中看到用户和 userId,但在流星方法中看不到(使用Meteor.userId()
    猜你喜欢
    • 2015-08-15
    • 2010-10-24
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多