javascript - React js,无法读取属性 "0"。调用API时

标签 javascript reactjs react-redux

我无法从获取函数访问数据。我想将数据从操作传递到 reducer 。使用fetch函数调用API,api以promise的形式返回。因此,API 被单独调用,数据返回到操作负载。

import { INDEX_PRESCRIPTION } from '../constant.js';

function fetch_prescription(){
  const base_url= "http://192.168.1.22:3000/api/v1/";
  const fetch_url = `${base_url}/prescriptions`;
  let datas = [];
  return fetch(fetch_url, {
    method: "GET"
  })
  .then(response => response.json())
  .then(data => {
    datas.push(data['prescriptions'])
    return datas
  })
}

export const indexPrescription = async () => {
  const action = {
    type: INDEX_PRESCRIPTION,
    details: await fetch_prescription()
  }
  return action;
  console.log('action details', action.details)
}

export const getIndexPrescription = (dispatch) => {
   dispatch(indexPrescription());
}

在检查控制台时,我们得到:

enter image description here

如何访问处方详细信息。我尝试通过 action.details["0"]["0"] 访问它,但结果是“无法读取未定义的属性“0””。我已经解决了与此问题相关的许多问题和解决方案,但无法研究我的代码出了什么问题。

更新这是我的index.jsx组件

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

class Index extends Component {
  constructor(props){
    super(props);
    this.state = {
      prescription: null
    }
  }

  componentWillMount(){
    this.props.getIndexPrescription();
  }

  render(){
    return(
      <h2>
        Prescription Index
      </h2>
    )
  }
}

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

function mapStateToProps(state){
  return {
    prescription: state
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Index);

我的 src/index.js 文件是

import React from 'react';
import ReactDOM from 'react-dom';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import {Provider} from 'react-redux';
import reducer from './reducers';
import Index from './components/index.jsx';

const store = createStore(reducer, applyMiddleware(thunk));

ReactDOM.render(
 <Provider store={store}>
    <Index />
 </Provider>, document.getElementById("root")
)

最佳答案

只有在你得到服务器的答复后,你的 promise 才会得到解决。您需要使用额外的层来处理 redux 中的异步行为。

例如使用redux-thunk,你可以让它像这样工作:

import { INDEX_PRESCRIPTION } from '../constant.js';
    
function fetch_prescription(){
  const base_url= "http://192.168.1.22:3000/api/v1/";
  const fetch_url = `${base_url}/prescriptions`;
  let datas = [];
  return fetch(fetch_url, {
    method: "GET"
  })
  .then(response => response.json())
  .then(data => data['prescriptions']);
}
    
export const indexPrescription = (dispatch) => {
  fetch_prescription()
     .then(details => {
          const action = {
              type: INDEX_PRESCRIPTION,
              details
           }
           
           dispatch(action);
  }
}

关于javascript - React js,无法读取属性 "0"。调用API时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496907/

相关文章:

reactjs - 当尝试映射react-redux状态时,state.get不是一个函数

javascript - 用于 Jquery 开发的 IDE

javascript - 如何在 Angular 2 中填充 p-dataTable?

reactjs - NextJS Googlebot 发生意外错误

javascript - 如何使用 React 在调整窗口大小时缩放 Highcharts 高度

javascript - 像在 IDE 中一样在文本区域中使用react自动完成(例如 VS Code、Atom)

javascript - jQuery 在 Click 上添加越来越多的项目

javascript - 删除特殊字符后未定义的数字javascript

redux - 带有 React.memo 的 useSelector 与连接

javascript - event.persist 不是函数 - JEST 和 Enzyme