I am trying to check if the username exists in users collection, In the instance of checking the data, function run faster than the call method(as you all know).This result to Session to become one step behind when the user is checking the username for availability.
I am stuck with these for a very long time, if you have a workaround for this I would very much appreciate it.
Here is the code:
const ChooseUsername = React.createClass({
getInitialState() {
return {
value: ''
};
},
getValidationState() {
const value = this.state.value;
Meteor.call("userExists", value, (err, userExists) =>{
if (err){
return err;
}
else{
return Session.set('result',userExists)
}
})
if(Session.get('result'))
return "error";
else return 'success';
},
handleChange(e) {
this.setState({ value: e.target.value });
},
render() {
return (<div id="signInPage">
<div id="tutorial">
<h4>Please Choose a Username</h4>
<form>
<FormGroup
controlId="formBasicText"
validationState={this.getValidationState()}
>
<FormControl
type="text"
value={this.state.value}
placeholder="Type your username"
onChange={this.handleChange}
/>
<FormControl.Feedback />
<HelpBlock>You can't change your username later</HelpBlock>
</FormGroup>
</form>
<p><a className="btn btn-success signupButtons" href="/" role="button">See tutorial</a></p>
</div></div>);
}
});
export default ChooseUsername;
Edit:
Just change the getValidationState() with the following code and it works like charm.
getValidationState() {
const value = this.state.value;
Meteor.call("userExists", value, (err, userExists) =>{
if (err){
return err;
}
else{
return this.setState({userIsThere:userExists})
}
})
const userIsThere = this.state.userIsThere
if(userIsThere)
return "error";
else return 'success';
},