我正在创建一个小型React Native应用程序,我想要获取Firestore中的“用户”集合中已经存在的用户数据,然后希望能够更新这些数据。但是我无法获取这些数据,并且总是显示“对象未定义”。
这是用户个人资料组件:
//thats the modal component that will handle the update
import UserDetails from '../src/components/userDetails'
//this func to send data to the modal component, but it didn't work!
handleUpdate = (id, nom, prenom, address) => {
this.setState(
{
id:id,
nom: nom,
prenom: prenom,
address: address,
}
)
}
//this func to get data from firebase, but it didn't work!
componentWillMount(props) {
const { user } = this.props ;
this.setState(
{
nom: user.nom,
prenom: user.prenom,
address: user.address,
}
)
}
render() {
const { user } = this.props.user ;
const update = user
? <UserDetails style={styles.modal} handleUpdate={this.handleUpdate(user.id, user.nom, user.prenom, user.address)} />
: <UserDetails style={styles.modal} />;
return (
<Block style={styles.profileTexts}>
<Text color="white" size={28} style={{ paddingBottom: 8 }}>{this.state.prenom}</Text>
<Text color="white" size={16} muted style={styles.seller}>{this.state.nom}</Text>
<Text size={16} color={'#FF9800'}>
4.8
</Text>
<Text color={theme.COLORS.MUTED} size={16}>
{` `} {this.state.address}
</Text>
</Block>
//this block will open the modal
<Block row space="between" style={{ flexWrap: 'wrap' }}
{update}
</Block>
</Block> ) ;
const mapStateToProps = (state) => {
console.log(state);
return{
auth: state.firebase.auth,
user: state.firebase.ordered.Users
}
}
const mapDispatchToProps = (dispatch) => {
return {
signOut: () => dispatch(signOut())
}
}
export default compose(
connect(mapStateToProps, mapDispatchToProps),
firestoreConnect([
{ collection: 'Users'},
]))(Profile)
这是模态组件(userDetails):
//thats the action component that will send data to firebase
import updateUser from '../actions/userAction';
saveUpdate=()=>{
this.props.updateUser({
nom: this.state.nom,
prenom: this.state.prenom,
address: this.state.address,
id: this.state.id
})
this.setState({
nom: "",
prenom: "",
address: "",
})
console.log("state after save update : ", state)
}
render() {
return (
<View style={styles.container}>
<TextInput
editable = {true}
maxLength = {40}
onChangeText={(text) => this.setState({nom: text})}
value={this.state.nom}
placeholder={"Nom"}
/>
<TextInput
editable = {true}
maxLength = {40}
onChangeText={(text) => this.setState({prenom: text})}
placeholder={"Prénom"}
value={this.state.prenom}
/>
<TextInput
editable = {true}
maxLength = {40}
onChangeText={(text) => this.setState({address: text})}
placeholder={'Address'}
value={this.state.address}
/>
</View>
<TouchableHighlight
style={styles.button}
onPress={() => { this.saveUpdate }}>
<Text style={styles.buttonTitle} >Save</Text>
</TouchableHighlight>
)}
const mapStateToProps = (state) => {
return {
users: state.firestore.ordered.Users
}
}
export default compose(
connect(mapStateToProps, {updateUser}),
firestoreConnect([
{ collection: 'Users'},
]))(UserDetails);
这是用户的操作:
import firestore from '../config/firebase_config'
const updateUser =(user)=>{
return (dispatch)=>{
console.log("trying to update: ", user);
firestore.firestore().collection("Users").doc(user.id)
.update(
{
nom: user.nom,
prenom: user.prenom,
address: user.address,
}
)
.then(() =>{
dispatch({
type:'UPDATE_USER',
})
})
.catch(function(error) {
console.error("Error updating document: ", error);
})
}
}
export default updateUser ;
我尝试了很多解决方案,并且花了很多时间,但是我无法弄清楚这里缺少什么。