0

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';
  },
mcnk
  • 1,690
  • 3
  • 20
  • 29
  • In your method callback, don't use Session, but rather the React state or emit an action that will be translated into the component props via a flux implementation. – MasterAM May 18 '17 at 15:56
  • You are absolutely right I fixed it by using setState. I will edit the code with the solution. Thank you very much. – mcnk May 19 '17 at 12:47

0 Answers0