首页 > 解决方案 > 我在减速器中的语法有什么问题?

问题描述

我正在尝试向我的减速器添加调度。但是,我遇到了语法问题。我不确定如何解决此错误。我在减速器中使用 POST 方法。

export function insertSearchTerm(searchTerm) {
   return (dispatch) => {
     fetch('http://localhost:3001/api/v1/searches?searchterm='+searchTerm {
        headers: {
          'Content-Type':'application/json',  
        },
        method:'POST',
        body: JSON.stringify(searchTerm)          searchTerm
      }).then(res => console.log('D') {
          dispatch({
             type:'INSERT_TOP_SEARCH',
             payload: res
          })
        }
      )
     }
   }


./src/actions/insertSearchTerm.js
  Line 4:  Parsing error: Unexpected token, expected ","

  2 | 
  3 |    return (dispatch) => {
> 4 |      fetch('http://localhost:3001/api/v1/searches?searchterm='+ searchTerm {
    |                                                                            ^
  5 |         headers: {
  6 |           'Content-Type':'application/json'
  7 |         },

标签: reactjsredux

解决方案


在 searchTerm 之后添加 ','

return (dispatch) => {
    fetch('http://localhost:3001/api/v1/searches?searchterm='+ searchTerm, {
        headers: {
       'Content-Type':'application/json'
    }
  }
}

推荐阅读