gpt4 book ai didi

javascript - 错误: network error and another error is "Type is not defined"

转载 作者:行者123 更新时间:2023-12-03 01:23:59 24 4
gpt4 key购买 nike

我正在开发天气 API 来制作天气显示应用程序..但我想运行这个项目,它在操作创建器中给出错误,其中显示类型未定义..

actions/index.js

import axios from 'axios';

const API_KEY = '6614d40c20e44e4e437b2b20c951ecc';
const ROOT_URL = `https://samples.openweathermap.org/data/2.5/forecast?appid=${API_KEY}`;

export const FETCH_WEATHER = 'FETCH_WEATHER';

export function fetchWeather(city){
const url = `${ROOT_URL}&q=${city},us`;
const request = axios.get(url);

return(
type: FETCH_WEATHER,
payload: request
);
}

我已经制作了一个搜索栏来搜索用户喜欢的城市..

containers/search.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { fetchWeather } from '../actions/index';

class SearchBar extends Component{
constructor(props){
super(props);

this.state = { term: '' };

this.onInputChange = this.onInputChange.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
}

onInputChange(event){
// console.log(event.target.value);
this.setState({ term:event.target.value });
}

onFormSubmit(event){
event.preventDefault();

// we need to go and fetch weather data
this.props.fetchWeather(this.state.term);
this.setState({ term: '' });
}

render(){
return(
<form onSubmit={this.onFormSubmit} className = "input-group">
<input
placeholder = "get a five-day forecast inypur city"
className = "form-control"
value = {this.state.term}
onChange = {this.onInputChange}/>
<span className = "input-group-btn">
<button type="submit" className="btn btn-secondary">submit</button>
</span>
</form>
)
}
}


function mapDispatchToProps(dispatch){
return bindActionCreators({ fetchWeather }, dispatch)
}

export default connect(null, mapDispatchToProps)(SearchBar);

每当我尝试搜索城市时,它都会显示类型未定义的错误,如下所示...

ERROR

最佳答案

  return(
type: FETCH_WEATHER,
payload: request
);

这并没有按照您的想法进行。

返回一个对象:

  return({
type: FETCH_WEATHER,
payload: request
});

关于javascript - 错误: network error and another error is "Type is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51634314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com