【发布时间】:2019-06-13 05:23:19
【问题描述】:
我正在尝试从 slack api 获取频道列表并将它们存储在 redux 存储中并做出反应。
axios 得到一个 [[PromiseValue]] ,但我无法获取其中的对象:
reducer 文件:
import * as actionTypes from "../actions/types";
import { getChannels } from "./../services/channelService";
const channelsReducer = (state = getChannels(), action) => {
switch (action.type) {
case actionTypes.GET_CHANNELS:
return state;
default:
return state;
}
};
channelService.js 文件:
import axios from "axios";
export const getChannels = async () => {
const token =
"token";
return await axios
.get(`https://slack.com/api/conversations.list?token=${token}`)
.then(response => {
return response.data;
});
};
action.js 文件:
import * as actionTypes from "./types";
export const getChannels = () => async dispatch => {
dispatch({
type: actionTypes.GET_CHANNELS
});
};
还有我的组件文件:
import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as actions from "./../actions/channelsActions";
class Navbar extends Component {
state = {};
componentDidMount() {}
render() {
console.log(this.props.channels);
}
}
const mapStateToProps = state => {
return {
channels: state.channels
};
};
const mapDispatchToProps = dispatch => {
return bindActionCreators(actions, dispatch);
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Navbar);
如何从 axios 访问这个 Promise 中的对象?
【问题讨论】:
标签: javascript reactjs redux promise