【问题标题】:Unexpected token when use Public keyword in javascript在 javascript 中使用 Public 关键字时出现意外的令牌
【发布时间】:2020-01-10 04:30:29
【问题描述】:

根据 Apollo 文档添加自定义指令如下:

class isAuth extends SchemaDirectiveVisitor {
    // field defination for resolving
    // directive.
    public visitFieldDefinition(field) {
        // overspreading.
        const { resolve = defaultFieldResolver } = field;

        // resolve field.
        field.resolve = async function(__, Context) {
            // check if Context
            // contain's user object
            // which was resolved by jwt.
            if(!Context.user){
                throw new Error('User not authenticated');
            }

            // resolve return awaited resolve call.
            return await resolve.apply(this, __);
        };
    }
}

但使用它会导致意外的令牌错误。

class isAuth extends SchemaDirectiveVisitor {
    // field defination for resolving
    // directive.
    visitFieldDefinition(field) { <---- Removed Public keyword
        // overspreading.
        const { resolve = defaultFieldResolver } = field;

        // resolve field.
        field.resolve = async function(__, Context) {
            // check if Context
            // contain's user object
            // which was resolved by jwt.
            if(!Context.user){
                throw new Error('User not authenticated');
            }

            // resolve return awaited resolve call.
            return await resolve.apply(this, __);
        };
    }
}

这将起作用,但不适用于 apollo 包。

请注意:Apollo 是 graphql 库

【问题讨论】:

    标签: javascript node.js apollo


    【解决方案1】:

    public 关键字存在于 JavaScript 中,但它是 reserved for future use

    因此,从 2019 年开始,任何 JavaScript 解析器都会给您带来意外的令牌错误。

    【讨论】:

    • Typescript 支持它。
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2021-06-22
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多