Axios-请求失败,状态码为400错误

问题描述 投票:0回答:1

我不知道为什么我尝试打yelp api时收到400错误的请求错误。

Request failed with status code 400
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\core\settle.js:16:9 in settle
- node_modules\axios\lib\adapters\xhr.js:52:6 in handleLoad
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:566:23 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

这是我在创建axios的代码,而不是提到的baseURL:

import axios from "axios";
const apiKey =
  "<API KEY HERE>";

export default axios.create({
  baseURL: "https://api.yelp.com/v3/businesses",
  headers: {
    Authorization: `Bearer ${apiKey}`
  }
});

我正在从另一个称为SearhcScreen的组件中搜索特定术语的请求:

import { View, Text, StyleSheet } from "react-native";
import SearchBar from "../components/SearchBar";
import yelp from "../api/yelp";

const SearchScreen = () => {
  const [term, setTerm] = useState("");
  const [results, setResults] = useState([]);

  const searchApi = async () => {
    try {
      const response = await yelp.get("/search", {
        params: {
          term,
          limit: 50,
          location: "karachi"
        }
      });
      setResults(response.data.businesses);
    } catch (e) {
      console.log(e);
    }
  };
  return (
    <View>
      <SearchBar
        term={term}
        onTermChange={newTerm => setTerm(newTerm)}
        onTermSubmit={() => searchApi()}
      />
      <Text>Search Screen</Text>
      <Text>We have found {results.length} results.</Text>
    </View>
  );
};

const styles = StyleSheet.create({});

export default SearchScreen;

我已经检查了yelp提供的API密钥是否正确,并在那里验证了我的开发人员帐户。但是从yelp api通过axios运行/检索列表仍然没有运气。

node.js react-native axios yelp
1个回答
0
投票

[Yelp.com无法找到您的位置karachi。因此,它们会根据您的get请求返回LOCATION_NOT_FOUND错误。

您可以通过将位置更改为New York进行测试。

yelp.get("/search", {
  params: {
    term,
    limit: 50,
    location: "New York"
  }
})

因此,请根据您的位置提供您的纬度和经度,以指定更准确的位置。确保将这些值提供为decimal

yelp.get("/search", {
  params: {
    term,
    latitude: 24.86,
    longitude: 67.01,
    limit: 50,
  }
});

希望这对您有所帮助。随时提出疑问。

© www.soinside.com 2019 - 2024. All rights reserved.