2017-07-30 52 views
1

嘿,我正在做一个非常简单的React应用程序项目,但我的编译器不断告诉我,那么是一个意外的令牌。我用npm install fetch --save安装了fetch api。它在我的package.json文件中被列为依赖项。。然后承诺不编译访存API

我的代码有问题吗?或者我必须安装一个npm包来使用.then方法。我的继承人代码

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)