【发布时间】:2019-07-15 17:58:40
【问题描述】:
我有一个节点服务器,它对存储在 MongoDB Atlas 数据库中的数据执行 CRUD 操作。我的前端是使用 react 制作的,我使用 redux 进行状态管理。在我进行后端设置之前,我通过调用我创建的仅返回 JSON 数据的函数来初始化 redux 存储的默认状态。现在我想通过 axios 向服务器发出一个 get 请求,以检索现在在 Mongo 数据库中的相同 JSON 数据。
我知道我应该在 componentDidMount 生命周期挂钩中进行 axios get 调用,但我的 store.js 不是一个类,但我不确定如何。然而,我能够执行 axios.get(URL) 但它以 [[PromiseStatus]]:"resolved", [[PromiseValue]]:{the data i want} 的形式返回一个对象。我读到这些是不可访问的。我想知道是不是因为我没有在生命周期中的正确位置或正确时间进行 axios 调用。
import { createStore } from "redux";
//import { syncHistoryWithStore } from "react-router-redux";
//import { browserHistory } from "react-router-dom";
import axios from "axios";
//import the root reducer.
import rootReducer from "../reducers/rootReducer";
//this is just getting JSON from a file
import { getAnnotations } from "../services/fakeEntry";
//create an object for default data
const defaultState = {
//This is how i was creating the default state before.
// metadata: getAnnotations()
//this is what id like to do.But this needs to be in a lifecycle hook?
metadata: axios
.get("http://localhost:5100/data/")
.then(response => {
return response;
})
.catch(function(error) {
console.log(error);
})
};
const store = createStore(rootReducer, defaultState);
export default store;
上面的代码是redux store。我已经使用邮递员测试了端点,它返回了 JSON 数据。当我在另一个组件中使用 mapStateToProps 读取这个 redux 状态时,我得到一个 Promise 对象。
【问题讨论】:
标签: node.js reactjs express redux axios