React-Redux语法错误","预期

时间:2017-04-30 12:27:14

标签: javascript reactjs react-redux

新手。

......这可能很简单。我查看了const" selectData"的代码。而且我找不到逗号的位置。这是整个文件:

export const requestLoginToken = (username, password) =>
  (dispatch, getState) => {
    dispatch({ type: REQUEST_LOGIN_TOKEN, payload: username })

    const payload = {
      userName: username,
      password: password,
    }

    const task = fetch('/api/jwt', {
      method: 'POST',
      body: JSON.stringify(payload),
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      },
    })
      .then(handleErrors)
      .then(response => response.json())
      .then(data => {
        dispatch({ type: RECEIVE_LOGIN_TOKEN, payload: data })
        saveJwt(data)

        selectData()

      })
      .catch(error => {
        clearJwt()
        dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message })
      })
    addTask(task)
    return task
  }

const selectData = () => {
  dispatch({ type: REQUEST_SELECT_DATA })

  const token = jwt.access_token
  const headers = new Headers({
    'Authorization': `Bearer ${token}`
  })
  const selectData = fetch('/api/SelectData/SelectData', {
    method: 'GET',
    headers,
  })
    .then(handleErrors)
    .then(response => response.json())
    .then(data => {
      dispatch({ type: RECEIVE_SELECT_DATA, payload: data })
        .catch(error => {
          clearJwt()
          dispatch({ type: ERROR_SELECT_DATA, payload: error.message })
        })
    }
}

错误发生在最后一个大括号上,它说:

  

意外的令牌,预期,(72:0)

第72行是最后一个花括号。

如果我删除了" selectData"的const表达式。没问题 - 没有错误。只有在我添加了代码块时才会出现错误...,即以下内容:

const selectData = () => {
  dispatch({ type: REQUEST_SELECT_DATA })

  const token = jwt.access_token
  const headers = new Headers({
    'Authorization': `Bearer ${token}`
  })
  const selectData = fetch('/api/SelectData/SelectData', {
    method: 'GET',
    headers,
  })
    .then(handleErrors)
    .then(response => response.json())
    .then(data => {
      dispatch({ type: RECEIVE_SELECT_DATA, payload: data })
        .catch(error => {
          clearJwt()
          dispatch({ type: ERROR_SELECT_DATA, payload: error.message })
        })
    }
}

为什么这段代码会导致错误?

1 个答案:

答案 0 :(得分:1)

您忘记了)上的then

.then(data => {
  dispatch({ type: RECEIVE_SELECT_DATA, payload: data })
    .catch(error => {
      clearJwt()
      dispatch({ type: ERROR_SELECT_DATA, payload: error.message })
    })
}) // <--- here

你应该始终使用;。我建议您使用linter检查代码,例如ESLint