2017-05-08 119 views
2

我在JS这个功能:与返回变量react方法

export let findUser = (user, pass) => fetch(baseURL+'/api/Login',{ 
    method: 'POST', 
     headers:{ 
      'Accept': 'application/json', 
      'Content-Type': 'application/json', 
     }, 
     body: JSON.stringify({ 
      username:user, 
      password:pass, 
     }) 
}) 
.then((response) => response.json()) 
.then((res) => { 
    if(res.success === true){   
      return 'A';   
    }else{  
     alert(res.message); 

    } 
}). 
.done(); 

但是,当我把它的阵营是这样的:

var user = file.findUser(this.state.username,this.state.password); 

我得到了不确定的变量用户的内容,怎么能我从第一个函数传递一个值并使其反应为原生的?

回答

0

我会建议导出初始方法调用并处理导出区域内的.then().done()。例如:

我在JS这个功能:

export let findUser = (user, pass) => fetch(baseURL+'/api/Login',{ 
    method: 'POST', 
     headers:{ 
      'Accept': 'application/json', 
      'Content-Type': 'application/json', 
     }, 
     body: JSON.stringify({ 
      username:user, 
      password:pass, 
     }) 
}) 

使用另一个文件

let user // assign user within response block 
file.findUser(this.state.username, this.state.password) 
    .then((res) => { /* do what you want */ }) 
    .then((res) => { /* do what you want */ }) 
    .done()