【问题标题】:function calling api returning empty array函数调用 api 返回空数组
【发布时间】:2022-12-19 01:01:56
【问题描述】:

我调用了该服务,但是当我运行 npm 时,我的浏览器上什么也没显示,错误是 401,空数组...

.ts 文件

const getHrRelation = async (
    auth: string,
    originId: string,
    location: string,
): Promise<HrRelation[]> => {
    try {
        const response = await axios.request({
            method: 'GET',
            url: `${process.env.API_GATEWAY}/hr/relation`,
            headers: {
                Authorization: auth,
            },
            responseType: 'json',
            params: {
                'api-version': apiVersion,
                origin: originId,
                location: location,

            },
        });
        return response.data as HrRelation[];
    } catch (err) {
        throw processAPIError(err, {
            resource: 'hr/relation',
            value: `location: ${location}`,
        });
    }
};

export default {
    getHrRelation,
};

列表文件

    const hrRelationsRes = await HrRelationProvider.getHrRelation(
            authorization,
            origin,
            location,
        );
        const hrRelations = rolesCodes
            ? hrRelationsRes.filter(elem => rolesCodes.indexOf(elem.role) > -1)
            : hrRelationsRes;

        if (hrRelations) {
            const locationListIds = Array.from(
                new Set(hrRelations.map(elem => elem.location) as string[])
            );

            const hrsId = Array.from(
                new Set(hrRelations.map(elem => elem.hr) as string[])
            );

这应该返回 hrs 列表,hr 关系本身......它怎么没有定义?

【问题讨论】:

    标签: typescript api service http-status-code-401


    【解决方案1】:

    问题出在原点没有正确调用服务。 我通过更改为文件中的正确语法来解决服务.ts

    getHRRelationRolesCodes(loc?) { const url = [api_myapi, 'hr'].join('/');

    const params = {};
    
    if (loc) {
        params['location'] = loc;
    }
    if (this.globals.selected_origin) {
        params['origin'] = this.globals.selected_origin;
    }
    
    const options = {
        headers: this.getHeaders(),
        params: params,
    };
    return this.http.get<any>(url, options).toPromise();
    

    }

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 2021-08-17
      • 2022-01-06
      • 2020-05-25
      相关资源
      最近更新 更多