从React Native中的另一个函数调用变量

时间:2018-04-19 15:52:15

标签: javascript react-native

大家好我试图从另一个文件中存在的函数中调用一些变量。该函数使用export所以我在其他js中导入它,最后新js的变量值返回undefined;

这里我创建user_id和token并发送到userCredentials()

export function loginUser(email, password) {
  return function (dispatch) {
    return axios.post(SIGNIN_URL, { email, password }).then((response) => {
      var { user_id, token } = response.data;
      dispatch(authUser(user_id));
      dispatch(userCredentials(user_id, token));
    }).catch((error) => {
      console.log(error);
      dispatch(addAlert("Could not log in."));
    });
  };
}

这是我从 app / actions / authActions.js

导出的功能
export function userCredentials(user_id, token) {
  user_id,
  token, 
  console.log('my token is ' + token); // I call this function when I Log In and generate the token value and it shows me in console.
}

现在我导入该功能并使用 app / actions / jobsActions.js

中的变量
import {userCredentials} from './authActions';


export function createJob(title, token, user_id) {
  return function (dispatch) {
    console.log('token is ' + token); // when I call this function here I get undefined
      return axios.post(JOBS_URL(user_id), { title }, {
        headers: { authorization: token }
      }).then((response) => {
        var { user_id, token } = credentials;
        dispatch(addJob(response.data.job));
      }).catch((err) => {
        dispatch(addAlert("Couldn't create job."));
      });
  };
}

0 个答案:

没有答案