I'm trying to print the user's info from firebase into placeholder of TextInput but when i try to set the info it isn't represented and node gives me a message:
Error:
Please update the following components: ProfileContent
LOG user logged
LOG -----O myemail.com
LOG -----O myname
LOG -----O mylastname
LOG -----O adriangc24
WARN Possible Unhandled Promise Rejection (id: 1):
TypeError: undefined is not an object (evaluating '_this2.setState')
My constructor:
constructor(props) {
super(props);
this.state = {
name: "",
lastname: "",
displayname: "",
email: "",
};
}
state = {
name: "Nombre",
lastname: "Apellidos",
displayname: "Nombre de usuario",
email: "Correo electrónico",
};
TextInput example:
<TextInput
autoCapitalize="sentences"
style={styles.userInput}
placeholder={this.state.lastname}
ref={"lastnamesInput"}
/>
My method for get the user's data from firebase:
function getUserData() {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("user logged");
var usuario = firebase.database().ref("/users/" + user.uid);
usuario.once("value").then((snapshot) => {
var usr = snapshot.val();
console.log("-----O " + usr.email);
console.log("-----O " + usr.name);
console.log("-----O " + usr.lastname);
console.log("-----O " + usr.displayname);
this.setState(
{
name: usr.name,
lastname: usr.lastname,
displayname: usr.displayname,
email: usr.email,
},
() => {
console.log("///////////////////////////////////////");
console.log("XXXXXX " + this.state.email);
}
);
});
} else {
console.log("user not logged");
}
});
}
It's the scope issue, due to function (user) {
you have lost the scope of this
, solution to this is use fat arrow function
.
Change this:
firebase.auth().onAuthStateChanged(function (user) {
To
firebase.auth().onAuthStateChanged((user) => {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.