Javascript 无法读取属性';获取';未定义的

Javascript 无法读取属性';获取';未定义的,javascript,node.js,graphql,apollo-server,Javascript,Node.js,Graphql,Apollo Server,我正在尝试向Apollo服务器添加REST数据源。 我创建了一个扩展RESTDataSource的类,该类包含所有API请求。 当尝试从我的GraphQL解析器代码调用login方法时,会抛出错误 我怎样才能解决这个问题 我已经试过了: 在API类构造函数中绑定login和fetch方法 this.login=this.login.bind(this); this.fetch=this.fetch.bind(this) 从构造函数调用login方法 RESTDataSource类 class A

我正在尝试向Apollo服务器添加REST数据源。 我创建了一个扩展RESTDataSource的类,该类包含所有API请求。 当尝试从我的GraphQL解析器代码调用login方法时,会抛出错误

我怎样才能解决这个问题

我已经试过了: 在API类构造函数中绑定login和fetch方法

this.login=this.login.bind(this); this.fetch=this.fetch.bind(this)

从构造函数调用login方法

RESTDataSource类

class API extends RESTDataSource {
        constructor() {
            super();
            this.baseURL = URL;

            this.login = this.login.bind(this);
            this.fetch = this.fetch.bind(this);
            console.log(this.fetch);
            console.log(this.login);

        }
        initialize(config) {
            //config can store different information
            //the current user can be stored in config.context for easy  access
            this.context = config.context;

        }

        async login(username, password) {
            return this.post(`/`, {
                "id": 1
            }, {
                "method": "authenticate"
            }, {
                params: {
                    "user": username,
                    "password": password
                },
                "jsonrpc": "2.0"
            })
        }
    }
以下是index.js apollo服务器“安装”文件:

const server=新服务器({
typeDefs,
解析器,
数据源:()=>{
返回{
managementDatabase:新的managementDatabase(),
API:new API()//以前已导入
}
}
});
server.listen()。然后(({
网址
}) => {
log(`解决了这个问题。
问题在于方法
initalize(config)
,在本例中,该方法是不必要的。
所以要解决这个问题,只需从代码中删除initalize方法

我写这篇文章是作为一个答案而不是注释,因为我没有足够的重复次数。 在本文中,提到了这一点。 他们的教程在克隆并运行时运行良好,但在我使用重写
初始化
实现它时,同样的情况不起作用。
但是,如果我不重写它,它就会像你提到的那样工作。

我们做错了吗?或者他们的文档需要更新?因为不正确的用户出现在
上下文中是一个严重的问题。

控制台.log(this.fetch);
在控制台中显示什么?输出是:
[函数:绑定获取]
哪个文件和行号引发此错误?@kmoser错误在
节点\u模块\\apollo datasource rest\\dist\\RESTDataSource.js:158:63)
。我还注意到我们在API类中打印的
this
。有一个名为
httpFetch
的属性未定义。当查看restdatasource.js文件时,抛出错误的行
const response=yield this.httpCache.fetch(请求,{..
使用这个.httpCache,它是在构造函数中这样创建的:
this.httpCache=new httpCache_1.httpCache(config.cache,this.httpFetch)
使用(我猜)未定义的httpFetch。但我不知道为什么