React Native Request失败,状态码为400

时间:2019-05-11 21:49:58

标签: react-native axios

任何人都有使用此API的经验: https://authenticjobs.com/api/docs#introduction

我已经在React Native项目中进行了几次实现迭代,而我最初遇到此错误:

  

未定义不是对象(正在评估'_ref.longitude')

现在我收到此错误:

  

请求失败,状态码为400

这是动作创建者,正在尝试记录此API请求的data对象。

import axios from "axios";
import reverseGeoCode from "latlng-to-zip";
import qs from "qs";

import { FETCH_JOBS } from "./types";

const JOB_ROOT_URL = "https://authenticjobs.com/api/?";
const JOB_QUERY_PARAMS = {
  api_key: "5634cc46389d0d872723b8c46fba672c",
  format: "json"
  // latlong: 1,
  // radius: 10,
  // q: "javascript"
};

const buildJobsUrl = zip => {
  const query = qs.stringify({ ...JOB_QUERY_PARAMS, l: zip });
  return `${JOB_ROOT_URL}${query}`;
};

export const fetchJobs = region => async dispatch => {
  try {
    let zip = await reverseGeoCode(region);
    const url = buildJobsUrl(zip);
    let { data } = await axios.get(url);
    dispatch({ type: FETCH_JOBS, payload: data });
    console.log(data);
  } catch (e) {
    console.log(e);
  }
};

1 个答案:

答案 0 :(得分:0)

虽然可以使我的应用程序朝着不同的方向发展,但我可以将其与该重构一起使用:

import axios from "axios";

import { FETCH_JOBS } from "./types";

const JOB_ROOT_URL = "https://authenticjobs.com/api/?api_key=";
const JOB_QUERY_PARAMS = {
  key: "a446a0eefe6f5699283g34f4d5b51fa0",
  method: "aj.jobs.getLocations",
  format: "json",
  category: "javascript"
};

export const fetchJobs = region => async dispatch => {
  try {
    const url =
      JOB_ROOT_URL +
      JOB_QUERY_PARAMS.key +
      "&method=" +
      JOB_QUERY_PARAMS.method +
      "&category=" +
      JOB_QUERY_PARAMS.category +
      "&format=" +
      JOB_QUERY_PARAMS.format;
    let { data } = await axios.get(url);
    dispatch({ type: FETCH_JOBS, payload: data });
    console.log(data);
  } catch (e) {
    console.log(e);
  }
};