【发布时间】:2019-07-17 14:40:39
【问题描述】:
我试图从我的数据库中获取一个嵌套的孩子,这是一个结构
我想从 Gallery 对象中获取 URI 当我登录时,我可以在控制台中看到这个
componentWillMount() {
const { gKey } = this.state;
firebase
.database()
.ref(`providers/${gKey}`)
.once("value")
.then(async snapshot => {
let aboutMe = snapshot.val().aboutMe;
let uri = snapshot.val().galary;
await this.setState({ aboutMe });
console.log(uri);
uri.forEach(childNodes => {
console.log(childNodes); // I Got Error here
});
});
}
我遇到的错误
uri.forEach 不是函数
更新
我在父“画廊”中获取了所有 URI,并将其设置在一个数组中,并将其作为道具传递给一个组件现在在这个组件中并在一个我只看到一个主题图像中呈现它!
父屏幕
Class Parent extend Component{
constructor(props){
super(props)
this.state={
...
images:[],
aboutMe:""
...
}
componentWillMount() {
const { gKey } = this.state;
firebase
.database()
.ref(`providers/${gKey}`)
.once("value")
.then(async snapshot => {
let aboutMe = snapshot.val().aboutMe;
let uri = snapshot.val().galary;
await this.setState({ aboutMe });
Object.values(uri).forEach(childNodes => {
let images = [];
images.push({
uri: childNodes.uri
});
this.setState({ images });
});
});
}
render(){
return(
<View>
....
<GalaryScreen images={this.state.images} />
....
</View>
)}
}
Galary.js
class GalaryScreen extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View style={styles.container}>
<Text style={{ fontSize: 20, padding: 10, color: "#000" }}>Galary</Text>
<FlatList
horizontal
key={Math.floor(Math.random() * 1000)}
data={this.props.images}
renderItem={({ item }) => {
console.log(item.uri); // I can see every url here and all of them is a vaild URL
return (
<View style={{ margin: 10 }}>
<Image
key={Math.floor(Math.random() * 100)}
style={{ width: 100, height: 100 }}
source={{ uri: item.uri }}
/>
</View>
);
}}
keyExtractor={(item, index) => item.key}
/>
</View>
);
}
}
export default GalaryScreen;
控制台
【问题讨论】:
-
能否为获取数据的组件添加代码,然后将其传递给 GalaryScreen 组件?
-
再检查一次,@TreyEckels
标签: javascript reactjs firebase react-native firebase-realtime-database