React-Redux 语法错误“,”预期

React-Redux syntax error "," expected

新手。

...这可能非常简单。我查看了 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 })
        })
    }
}

错误出现在最后一个大括号中,它表示:

Unexpected token, expected , (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 })
        })
    }
}

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

您忘记了 ) 最后一个 then:

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

而且您应该始终使用 ;。我建议你使用 linter 来检查你的代码,比如 ESLint