2017-07-06 78 views
1

权利,使我从父母传给一个数组下到孩子,我会包括以下阵营的js遍历父阵列用于登录授权

所有代码,所以我试图做的是循环在我的阵列并检查我的用户输入相匹配的数组中的对象之一,如果它不与代码继续,如果不给出提示错误

我父代码

... 
state = { 
    AuthResults: [ 
     { 
      id: 1, 
      username: "Andy", 
      password: "Andy" 
     }, 
     { 
      id: 2, 
      username: "Dan", 
      password: "Dan" 
     }, 
     { 
      id: 3, 
      username: "Naheem", 
      password: "Naheem" 
     } 
    ], 
} 
... 
<LoginBar AuthRes={this.state.AuthRes}/> 
... 

我对孩子守则

CheckLoginAuth(){ 
    console.log("gets to check login auth"); 
    console.log(this.state.AuthRes); 
    Object.keys(this.state.AuthRes).map(function(key){ 
     console.log(AuthRes[key]); 
    }) 

} 
... 
<TextField floatingLabelText="Username" name="Username" onChange={this.handleUsername} value={this.state.Username} type="text" /> 
<TextField floatingLabelText="Password" name="Password" onChange={this.handlePassword} value={this.state.Password} type="text" /> 
<RaisedButton label="Login" secondary={true} onClick={() => this.onRealLoginClick()} /><span>&nbsp;</span> 
... 

它将对象正确地拉动,因为它打印它,但我似乎无法做循环和身份验证。

编辑:对,所以我现在有没有错误,通过改变我的CheckLoginAuth功能,这

CheckLoginAuth(){ 
    console.log("gets to check login auth"); 
    console.log(this.state.AuthRes); 
    console.log("Username=" + this.state.Username); 
    console.log("Password=" + this.state.Password); 
    this.props.AuthRes.map(function(Res){ 
     console.log(Res.id); 
     console.log(Res.username); 
     console.log(Res.password); 
    }) 
} 

我如何遍历RES的ID,并将它们与用户输入

+1

写我没有看到你在哪里loopin g通过AuthRes ... –

+0

因为我需要检查用户从TextField的输入,看用户输入的是否与用户名和密码中的一个匹配作为登录文件的用户名和密码(显然数据最终会进入我的api,但是我不知道C#所以我必须等待其他开发人员之一,所以即时尝试现在解决它 –

+0

@JonB进出口没有循环,因为一切都试图没有工作即时通讯要求如何做到这一点。我将在CheckLoginAuth功能中执行此操作。 –

回答

0

我解决了这个问题通过做

CheckLoginAuth() { 
    console.log("gets to check login auth"); 
    console.log(this.state.AuthRes); 
    console.log("Username=" + this.state.Username); 
    console.log("Password=" + this.state.Password); 
    var self = this; 
    this.props.AuthRes.map(function (Res) { 
     console.log("ID=" + Res.id); 
     console.log("Username=" + Res.username); 
     console.log("Password=" + Res.password); 
     self.LoginAuth(Res); 
    }); 
} 

什么,而不是我上面