Axios返回一个奇怪的数组以响应请求

时间:2019-11-27 21:26:00

标签: javascript arrays json react-native axios

我正在尝试对数据库进行请求;我使用userid作为属性来过滤响应。使用的“ myid”来自我的redux。执行此请求时,我有一个有点奇怪的数组作为答案,而用户发表的帖子我没有正确的答案。

请求响应:

enter image description here

import axios from 'axios'
import { NavigationEvents } from 'react-navigation'
import Post from '../components/Posts/post'

const mapStateToProps =({user}) => {
  return {
    email:user.email,
    name:user.name,
    myid:user.id
  }
}

 class MyPosts extends Component {

  state={
    posts:[],
  }

  getPosts  = () =>{
    console.log(this.props.myid)
    axios.get(`/posts?userid=${this.props.myid}.json`)
      .then( res => {
        console.log(res)
        const rawPosts = res.data
        const posts =[]
          for (let key in rawPosts) {
            posts.push({
              ...rawPosts[key],
              id: key
            })
          }
          this.setState({ posts: posts})
        }
      )
      .catch (err => console.log(err))

  }
  componentDidMount = () => {
    this.getPosts()
  }

  render () {


    return (
      <View style={styles.containerAll}>
        <NavigationEvents onDidFocus={() => this.getPosts()} />
        {console.log('Log do Return', this.state.posts)}
        <View>
          <Header style={{ backgroundColor: '#006bb3' }}>
            <Left>
              <Icon
                name="md-menu"
                type='Ionicons'
                style={{ color: "#FFF" }}
                onPress={() => this.props.navigation.openDrawer()}
              />
            </Left>
            <Body>
              <Title>Meus anúncios</Title>
            </Body>
            <Right>

            </Right>
          </Header>
        </View>
        <View style={styles.container}>
          <View style={{marginLeft:0, marginTop:15}}>
            <FlatList
              data={this.state.posts}
              keyExtractor={item =>`${item.id}`}
              renderItem={({item}) =>
              <TouchableOpacity 
                activeOpacity={0.7}
                onPress={() => this.props.navigation.navigate('Detail',{item :item})}>
                <Post key={item.id} {...item} />
              </TouchableOpacity>
              }
            />
          </View>
        </View>
      </View>
    )
  }
}
export default connect(mapStateToProps) (MyPosts)

我正在使用:

  • react-redux”:7.1.1,
  • redux”:4.0.4,
  • 反应”:16.8.6,
  • 本机“:0.60.4”,
  • axios”:0.19.0,

1 个答案:

答案 0 :(得分:1)

要使用不带Firebase的过滤器进行查询,必须使用一些不同的参数。我没有使用这些参数,所以我得到了这个奇怪的数据。要正确执行查询,需要对其进行修改。因此,我们有:

axios.get(`/posts.json?orderBy="userid"&equalTo="${this.props.myid}"`)