web apiであるURLを用いることができません。
以下に該当するコードを載せさせていただきます。ご助力よろしくお願いします。。。
js
1import React, { Component } from 'react'; 2import Header from './containers/Header'; 3import Main from './containers/Main'; 4 5const endPoint = new URL(process.env.REACT_API_PATH); 6 7class App extends Component { 8 constructor(props){ 9 super(props); 10 this.state ={ 11 id: '', 12 title: '', 13 description: '', 14 price: '', 15 image: '', 16 data: [] 17 }; 18 this.handleGetProduct = this.handleGetProduct.bind(this); 19 this.handlePost = this.handlePost.bind(this); 20 } 21 22 handleGetProduct(keyword){ 23 let url = endPoint; 24 if(keyword){ 25 url = endPoint+ '/search?keyword=' + keyword; 26 } 27 fetch(url, { 28 method: "GET", 29 headers: { 30 "Content-Type": "application/json" 31 } 32 }).then(response => { 33 response.json().then(data => { 34 (this.setState({data:data})) 35 }) 36 }) 37 } 38 39 handlePost(event, title, description, price){ 40 fetch(endPoint, { 41 method: "POST", 42 headers:{ 43 "Content-Type": "application/json" 44 }, 45 body: JSON.stringify({title: title, description: description, price: price, image: null}) 46 }) 47 .then(response => { 48 response.json().then(() => { 49 this.handleGetProduct() 50 }) 51 }) 52 } 53 54 55 56 render() { 57 const products = this.state.data; 58 return ( 59 <div className="App"> 60 <Header/> 61 <Main onGet={this.handleGetProduct.bind(this)} onPost={this.handlePost.bind(this)} products={products}/> 62 </div> 63 ); 64 } 65} 66 67export default App;
.envファイルの中身について
REACT_API_PATH='http://localhost:8080/api/product' REACT_GITHUB_PATH='https://localhost:8080/github/login' REACT_GITHUB_LOGOUT_PATH='https://localhost:8080/github/logout'
errorメッセージについて
Failed to construct 'URL': Invalid URL
回答1件
あなたの回答
tips
プレビュー