问题:我正在尝试使用RESTDataSource
的{{1}}方法发出GET请求,但是收到get
错误。
我的代码:
ERR_INVALID_URL
预期:请求成功,完整网址为:
async getMediaIDs() {
let response;
try {
response = await this.get(`${process.env.INSTA_ID}/media`,{
access_token: `${process.env.PAGE_accESS_TOKEN}`,});
} catch(err) {
throw new Error(err);
}
return response.data.data;
}
实际:我收到此错误:
https://graph.facebook.com/{insta_id}/media?access_token={access_token}
我怀疑错误所在
2019-11-15T10:03:42.974335+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Error: TypeError [ERR_INVALID_URL]: Invalid URL: 17841402041188678/media
2019-11-15T10:03:42.974379+00:00 app[web.1]: at InstagramAPI.getMediaIDs (/app/src/data/instagram.js:17:13)
2019-11-15T10:03:42.974382+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:93:5)
2019-11-15T10:03:42.974395+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
2019-11-15T10:03:42.974467+00:00 app[web.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
因为我没有正确设置this.get(`${process.env.INSTA_ID}/media`,{
access_token: `${process.env.PAGE_accESS_TOKEN}`,});
参数( ...我想。我不知道。)
在寻找解决方案时,我发现了params
文件,该文件导致了这一情况:
RESTDataSource
遵循protected async get<TResult = any>(
path: string,params?: URLSearchParamsInit,// <----- (What I'm trying to set)
init?: RequestInit,): Promise<TResult> {
return this.fetch<TResult>(
Object.assign({ method: 'GET',path,params },init),);
}
会导致以下结果:
URLSearchParamsInit
我对TypeScript不太熟悉,但是我猜想这是定义export type URLSearchParamsInit =
| URLSearchParams
| string
| { [key: string]: Object | Object[] | undefined }
| Iterable<[string,Object]>
| Array<[string,Object]>;
的方法。
无论如何,我的问题是如何为params?
的{{1}}方法设置params
参数?
Apollo开发人员的旁注:RESTDataSource
的API页面非常出色!由于目前尚无合适的文档,我愿意提供帮助。