react-redux:未处理的拒绝(TypeError):无法读取未定义的属性“ data”

时间:2019-12-08 14:36:55

标签: reactjs redux routes react-redux fetch-api

因此,我尝试使用axios从API提取数据,当我在前端代码上“ npm start”时,它返回未处理拒绝(TypeError):无法读取未定义的属性“ data”。我试图找出问题所在,但我不知道。

这是我的后端代码:

// //config
require('dotenv').config()
const express = require('express')
const app = express()
const mongoose = require('mongoose')

var cors = require('cors');
app.use(cors());
mongoose.connect(process.env.DATABASE_URL,
  {
    useCreateIndex: true,
    useNewUrlParser: true,
    useUnifiedTopology: true
  }
)

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  next();
})


const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', ()=> console.log('connected'))

const gasRouter = require('./routes/gas-routes')
app.use('/api/gas', gasRouter)

const stockRouter = require('./routes/stock-routes')
app.use('/api/stock', stockRouter)
//
// const wisataPrimer = require("./routes/route.js")
// app.use('/')
app.listen(5001,() => console.log('server started'))

这是我的前端(操作)代码:

import {GET_GASTYPE, GET_STOCKTABLE} from './actionTypes';
import axios from 'axios'

export const getGasType = () => dispatch => {
    axios.get('/api/gas')
    .then(res =>
        dispatch({
        type: GET_GASTYPE,
        payload: res.data
    }))
    .catch(err => 
    dispatch((err.response.data, err.response.status))
);
};

export const getGasStatus = () => dispatch => {
    axios.get('/api/stock')
        .then(res =>
            dispatch({
            type: GET_STOCKTABLE,
            payload: res.data
        }))
    .catch(err => 
        dispatch((err.response.data, err.response.status))
    );
};

你们能告诉我代码中哪一部分错误吗?谢谢。

1 个答案:

答案 0 :(得分:0)

问题可能出在减速机上,请检查您要更新的状态是否正确