2017-05-08 19 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()