Once I want to join a new group and click on the JOIN button on first click, the members array of that group is empty when it goes to check if a members already exists. Everything works after first click, but not on first click...
memberAlreadyExistsInGroup(email) {
for (var i = 0; i < this.state.members.length; i++) {
if (email === this.state.members[i].user_id) return true; //does not exist
}
return false; //exists
}
joinGroup = (id, email, name) => {
//make it so if user already exists in the group, alert(You cannot join cause you are already inside this group)
fetch(`http://localhost:8000/groups/${id}/members`) //gets all the members inside the group
.then(res => res.json())
.then(json => {
this.setState({
members: json
});
});
console.log(this.state.members); //BUG: the array is empty on first click of join button
if (this.memberAlreadyExistsInGroup(email) === false) {
//sees if member already exists
fetch(`http://localhost:8000/groups/${id}/members`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
user_id: email,
admin: false
})
});
//give access to the group afterwards
//this.sendGroupRequest(email, id); //send a group request to the database
alert("You have joined " + name + "'s group !");
//window.location.replace("/group/" + id);
} else {
//if the email is the same as one of the emails in the group members list
alert("You are already a member of this group !");
}
};
Once I want to join a new group and click on the JOIN button on first click, the members array of that group is empty when it goes to check if a members already exists. Everything works after first click, but not on first click...