这是我的减速器:
export default function(state=[], action){
switch (action.type) {
case FETCH_PLACES:
return [action.payload.data];
}
return state;
}
这是我的飞行容器:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import moment from 'moment';
class FlightList extends Component {
renderFlights(flightData){
const result = flightData.data.length;
console.log(flightData);
var rows = [];
for (var i = 0; i < result; i++) {
rows.push(
<tr key={i} >
<td>{i+1}</td>,
<td>{flightData.data[i].airlines[0]}</td>,
<td>{flightData.data[i].route[0].flight_no}</td>,
<td>{flightData.data[i].mapIdfrom}</td>,
<td>{flightData.data[i].mapIdto}</td>,
<td>
{
moment.unix(flightData.data[i].dTime).format("YYYY-MM-DD HH:mm")
}
</td>,
<td>
{
moment.unix(flightData.data[i].aTime).format("YYYY-MM-DD HH:mm")
}
</td>,
<td>{flightData.data[i].conversion.EUR} Euro</td>
</tr>
);
}
return <tbody>{rows}</tbody>;
}
render(){
return (
<table className="table table-hover">
<thead>
<tr>
<th>#</th>
<th>Airline</th>
<th>Flight Number</th>
<th>From</th>
<th>To</th>
<th>Departure</th>
<th>Arrival</th>
<th>Cost</th>
</tr>
</thead>
{this.props.flights.map(this.renderFlights)}
</table>
);
}
}
const mapStateToProps=({ flights })=>{
return { flights };
};
export default connect(mapStateToProps)(FlightList);
在redux-thunk
我尝试运行我的应用程序时使用后,我收到以下错误:
未捕获的类型错误:无法读取未定义的属性“数据”
我试图找出造成这种情况的原因,如果在我的减速器上我更改[action.payload.data]
为[action.payload]
然后我得到错误:
未捕获的类型错误:无法读取未定义的属性“长度”
这是我的 src/index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
//import ReduxPromise from 'redux-promise';
import thunk from 'redux-thunk';
import App from './components/app';
import reducers from './reducers';
//const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
const store = createStore(reducers,applyMiddleware(thunk));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.querySelector('.container'));
但无论我总是得到一个 JSON 文件,其中包含我需要的所有信息,并且在我的容器中,我尝试以表格格式发布它们。我不明白是什么原因造成的。有没有办法在不将我的源文件更改为以前的文件的情况下解决这个问题?
更新
import axios from 'axios';
import moment from 'moment';
const ROOT_URL = `https://api.skypicker.com/flights`;
//to keep out action type consistent between our action creator and our action reducers
export const FETCH_PLACES = 'FETCH_PLACES';
export function createQueryString(obj){
const Qstring = require('querystring');
const newQuery = Qstring.stringify(obj);
return newQuery;
}
export function fetchPlaces(query) {
let skypicker = {
flyFrom: query.from,
//to: query.to,
dateFrom: query.startDate,
dateTo: query.finishDate,
price_to: query.budget,
partner: "picky"
//partner_market: "yout id"
};
const FormattedDateFrom = moment(skypicker.dateFrom).format('DD/MM/YYYY');
const FormattedDateTo = moment(skypicker.dateTo).format('DD/MM/YYYY');
skypicker.dateFrom = FormattedDateFrom;
skypicker.dateTo = FormattedDateTo;
const queryString = createQueryString(skypicker);
const url = `${ROOT_URL}?${queryString}`;
const request = axios.get(url);
return {
type: FETCH_PLACES,
payload: request
};
}