简体   繁体   English

Yelp Api 业务搜索无法使用 axios 在 React Native 中工作 - 返回一个空数组

[英]Yelp Api business search not working in react native using axios - returns an empty array

Having trouble getting the yelp api to work, it is supposed to search for businesses based on the set of term.无法让 yelp api 正常工作,它应该根据术语集搜索企业。 I am using a async await function to GET the businesses/search object, then I am setting my params then updating state using setResults我正在使用异步等待 function 获取业务/搜索 object,然后设置我的参数,然后使用 setResults 更新 state


 import React, { useState } from "react";
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([]);

console.log(results);
console.log(term);

const searchApi = async () => {
 try {
   const response = await yelp.get("/search", {
     params: {
       limit: 50,
       term,
       location: "san jose",
     },
   });
   setResults(response.data.businesses);
 } catch (err) {
   console.log(err);
 }
};

return (
 <View style={styles.background}>
   <SearchBar term={term} onTermChange={setTerm} onTermSubmit={searchApi} />
   <Text>We have found {results.length} results </Text>
 </View>
);
};

const styles = StyleSheet.create({});

export default SearchScreen;

Here is the SearchBar component这是 SearchBar 组件

import React from "react";
import { View, TextInput, StyleSheet } from "react-native";
import { Feather } from "@expo/vector-icons";

const SearchBar = ({ term, onTermChange, onTermSubmit }) => {
  return (
    <View style={styles.backgroundStyle}>
      <Feather name="search" style={styles.iconStyle} />
      <TextInput
        autoCapitalize="none"
        autoCorrect={false}
        style={styles.inputStyle}
        placeholder="Search"
        value={term}
        onChangeText={onTermChange}
        onEndEditing={onTermSubmit}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  backgroundStyle: {
    backgroundColor: "#FFF",
    height: 50,
    borderRadius: 5,
    marginHorizontal: 15,
    top: 15,
    flexDirection: "row",
    marginBottom: 20
  },
  inputStyle: {
    flex: 1,
    borderRadius: 5,
    fontSize: 18,
  },
  iconStyle: {
    fontSize: 35,
    alignSelf: "center",
    marginHorizontal: 15,
  },
});

export default SearchBar;

And here is where I used axios to set the baseUrl and header这是我使用 axios 设置 baseUrl 和 header 的地方

import axios from "axios";

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

Any Help is greatly appreciated I console.logged the results and term to make sure all my state is working and it is so it has to be some issue with how i'm getting the yelp api. My results always return an empty array.非常感谢任何帮助我控制台记录了结果和术语,以确保我的所有 state 都正常工作,所以它必须是我如何获得 yelp api 的一些问题。我的结果总是返回一个空数组。

When Console logged登录控制台时

the {result} just shows an empty array the starting state – {result} 只显示一个空数组开始 state –

the {term} shows the state of whatever has been typed {term} 显示输入的任何内容的 state

Expected result预期结果

the {result} is supposed to show an arrays of objects matching "pasta" in the San Jose area and the number in the Text is supposed to show results.length {result} 应该显示 arrays 个与圣何塞地区的“意大利面”匹配的对象,文本中的数字应该显示 results.length

I tried your repo.我试过你的回购。 At first I got a 401 error because of no space in the auth token起初我收到401错误,因为身份验证令牌中没有空格

sdk_gphone_x86_arm:
Error: "Request failed with status code 401" in 

Then I got 400 for an invalid token然后我得到了400的无效令牌

sdk_gphone_x86_arm:
start
sdk_gphone_x86_arm:
Error: "Request failed with status code 400" in 

After corrected both, the app runs successfully, you can try it in https://snack.expo.dev/@anthnoycc/searchscreen两者更正后,应用运行成功,您可以在https://snack.expo.dev/@anthnoycc/searchscreen中尝试

hey i am learning react native from the same course and in my opinion the api key we are using is expired we have to get a new one in order for it to retrieve some data嘿,我正在同一门课程中学习 React Native,我认为我们使用的 api 密钥已过期,我们必须获得一个新密钥才能检索一些数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM