我正在尝试向我的数据库发出请求;我使用 userid 作为属性来过滤响应。使用的“myid”来 self 的 redux。执行此请求时,我有一个有点奇怪的数组作为答案,并且我没有用户发布的帖子的正确答案。
请求响应:
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/