【发布时间】:2020-03-11 11:40:47
【问题描述】:
问题:我正在尝试使用 RESTDataSource 的 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;
}
预期:请求成功,完整的url为:
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 会导致这个:
export type URLSearchParamsInit =
| URLSearchParams
| string
| { [key: string]: Object | Object[] | undefined }
| Iterable<[string, Object]>
| Array<[string, Object]>;
我对 TypeScript 不太熟悉,但我猜这些是定义 params? 的方法。
无论如何,我的问题是如何为RESTDataSource 的get 方法设置params 参数?
Apollo 开发者的旁注:RESTDataSource 的 API 页面会很棒!我愿意帮助记录它,因为目前没有合适的文档。
【问题讨论】:
-
该错误的完整堆栈跟踪是什么?由于该错误是由 Node 本身引发的,因此您也可能只是在 URL 字符串中提供了无效字符。
-
@DanielRearden 我已经添加了完整的错误日志!我会调查一下,谢谢。
标签: javascript node.js typescript apollo apollo-server