2017-07-30 6 views
1

ちょっと私はかなり簡単なReact Applicationプロジェクトをやっていますが、私のコンパイラはそれが予期せぬトークンです。 netch install fetch -saveを使ってフェッチapiをインストールしました。そして、それは私のpackage.jsonファイル内の依存関係としてリストされています。.then fetch apiをコンパイルしないと約束します

私のコードに何か問題がありますか?または.thenメソッドを使用するには、npmパッケージをインストールする必要があります。私のコード

import React from 'react'; 
import {FormGroup, FormControl, InputGroup, Glyphicon} from 'react-bootstrap'; 

class Global extends React.Component { 
    constructor(props) { 
    super(props) 
    this.state = {query: ''}; 
    } 
    search() { 
    const BASE_URL = 'https://www.googleapis.com/books/v1/volumes?q='; 
    fetch(`${BASE_URL}${this.state.query}`, {method: 'GET'}); 
    .then(response => response.json()); 
    .then(json => console.log(json)) 
    console.log('search', this.state.query) 
    } 

    render() { 
    return(
    <div className="global"> 
     <h2> Book Explorer!</h2> 
     <FormGroup> 
     <InputGroup> 
     <FormControl type="text" placeholder="search for a book" onChange={event => this.setState({query: event.target.value})} onKeyPress={event => { 
      if(event.key === 'Enter') { 
      this.search(); 
      } 
      }}/> 
     <InputGroup.Addon onClick={() => this.search()}> 
     <Glyphicon glyph="search"></Glyphicon> 
     </InputGroup.Addon> 
     </InputGroup> 
     </FormGroup> 
     </div> 
    ) 
    } 
} 

export default Global; 

相続人はここにあなたが置くことによって文を終わらせるべきではありませんまた、package.json

"devDependencies": { 
    "babel-core": "^6.2.1", 
    "babel-loader": "^6.2.0", 
    "babel-preset-es2015": "^6.1.18", 
    "babel-preset-react": "^6.1.18", 
    "chai": "^3.5.0", 
    "chai-jquery": "^2.0.0", 
    "jquery": "^2.2.1", 
    "jsdom": "^8.1.0", 
    "mocha": "^2.4.5", 
    "react-addons-test-utils": "^0.14.7", 
    "webpack": "^1.12.9", 
    "webpack-dev-server": "^1.14.0" 
    }, 
    "dependencies": { 
    "babel-preset-stage-1": "^6.1.18", 
    "fetch": "^1.1.0", 
    "lodash": "^3.10.1", 
    "react": "^0.14.3", 
    "react-bootstrap": "^0.31.1", 
    "react-dom": "^0.14.3", 
    "react-redux": "4.3.0", 
    "react-router": "^2.0.1", 
    "redux": "^3.0.4" 
+1

'末尾を削除し、' '(' $ {BASE_URL} $ {this.state.query} '、{方法: 'GET'})をフェッチ;'両方の行の後に。 ** **を使用して何かをチェーンしたい場合は、ドットの前にセミコロンを入れてはいけません – baao

答えて

1

内の私の依存関係です「;」。約束は、単一の文に連鎖されるべきである:

const BASE_URL = 'https://www.googleapis.com/books/v1/volumes?q='; 

    fetch(`${BASE_URL}${this.state.query}`, {method: 'GET'}) 
    .then(response => response.json()) 
    .then(json => console.log(json)); 

    console.log('search', this.state.query) 
関連する問題