在 react native 中开发应用程序我尝试使用客户端的数据和产品类型发出动态警报,但是当我尝试构建连接字符串的变量时,我收到此错误。
类型错误:未定义不是对象(评估“this.state.Gas”)
这是我的代码的相关部分:
使成为:
render() {
return (
<ScrollView style={styles.container}>
<Text style={styles.Titulo}>Pide tu cilindro!</Text>
<View style={styles.cuadros}>
<View style={styles.gas}>
<Image
source={require("./assets/licuado-2kg.png")}
style={{ height: 170, width: 170 }}
/>
</View>
<View style={styles.texto}>
<Text style={styles.cilindros}>GASCO 2 kg</Text>
<TouchableOpacity
style={styles.btnpedir}
onPress={this.confirm}
>
<Text style={styles.pedir}>Pedir</Text>
</TouchableOpacity>
</View>
</View>
</ScrollView>
);
}
}
构造函数:
constructor(props) {
super(props);
this.state = {
email: "",
displayName: "",
Ciudad: "",
Comuna: "",
Direccion: "",
Gas: "GASCO 2 kg"
};
}
功能:
confirm() {
let variable = this.state.Gas
let datos = this.state.displayName + this.state.Ciudad + this.state.Comuna + this.state.Direccion
let texto = "Deseas confirmar el pedido de: ".concat(variable, " a: ", datos)
Alert.alert(
"Confirmar pedido",
texto,
[
{
text: "Confirmar",
onPress: () => console.log("Confirmar"),
},
{
text: "Editar datos",
onPress: () => console.log("Editar"),
},
{
text: "Cancelar",
onPress: () => console.log("Cancelar"),
style: "cancel",
}
],
{ cancelable: false }
);
}
组件DidMount:
componentDidMount() {
const { email, displayName } = firebase.auth().currentUser;
this.setState({ email, displayName });
firebase
.firestore()
.collection("Users")
.where("Email", "==", email)
.get()
.then((snapshot) => {
this.setState({ Doc: snapshot.docs[0].id });
firebase
.firestore()
.collection("Users")
.doc(this.state.Doc)
.get()
.then((doc) => {
this.setState({
Ciudad: doc.data().City,
Direccion: doc.data().Direction,
Comuna: doc.data().Comuna,
});
});
});
};
我该如何解决这个问题??