javascript - axios响应请求返回一个奇怪的数组

标签 javascript arrays json react-native axios

我正在尝试向我的数据库发出请求;我使用 userid 作为属性来过滤响应。使用的“myid”来 self 的 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,
  • react ”:16.8.6,
  • react native “:0.60.4”,
  • axios":0.19.0,

最佳答案

要在不使用 Firebase 的情况下使用过滤器进行查询,需要使用一些不同的参数。我没有使用这些参数,所以我得到了这些奇怪的数据作为响应。要正确执行查询,需要对其进行修改。因此我们有:

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

关于javascript - axios响应请求返回一个奇怪的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078656/

相关文章:

javascript - Js中阻止函数多次运行的最佳方法是什么

javascript - 在 javascript 中使用 ModelAndView 对象属性

javascript - 如何将JSON数据推送到二维数组中?

javascript - 在javascript中按值作为键创建新对象

javascript - AngularJS 将递归 JSON 解析为对象数组

javascript - Webpack 编译的 Chrome 扩展抛出 `unsafe-eval` 错误

javascript - Draft.js 中的自定义可编程小部件(插件)

javascript - 如何检查字符串是否包含 JavaScript 中子字符串数组中的文本?

ios - 解析 JSON Swift

java - jelly:使用 JSON 对象创建表