嘿,我尝试从nodejs服务器获取数据以响应Project中的服务器,即使我的操作和reducer工作正常,但我的数据中却出现空字符串:“”,而不是用户的googleID
这是我的下面的代码
Actions.js
import axios from "axios";
export const fetchuser = () => {
return async(dispatch)=>{
const res=await axios.get("http://localhost:5000/api/currentuser")
dispatch({
type:'fetchuser',
payload:res
})
}
};
App.js
import React, { Component } from "react";
import { BrowserRouter, Route } from "react-router-dom";
import {connect} from 'react-redux'
import Header from "./Header";
import * as actions from '../actions'
class App extends Component {
componentDidMount() {
this.props.fetchuser()
}
render() {
return (
<div className="container">
</div>
);
}
}
export default connect(null,actions)(App)
authreducer.js
export default function(state={},action){
console.log(action)
switch(action.type){
default:return state
}
}
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import reducers from './reducers'
import 'materialize-css/dist/css/materialize.min.css'
import reduxThunk from 'redux-thunk'
const store = createStore(reducers,{}, applyMiddleware(reduxThunk));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
减速器/索引
import {combineReducers} from 'redux'
import authreducer from './authreducer'
export default combineReducers({
auth:authreducer
})
我正在使用npm cors()软件包来从localhost:3000(反应服务器)向localost:5000 / api / currentuser(nodejs服务器)发出请求
因此,当我直接访问http://localhost:5000/api/currentuser时,我会看到数据{"_id":"5d75807cbd0d3f3ce487a704","googleid":"109463598810933991924","__v":0}
但是从我的反应方面来说,当我在componentdidmount中调用此URL并在authreducer.js的控制台上打印时 它给了我空数据
{type: "fetchuser", payload: {…}}
payload:
config: {url: "http://localhost:5000/api/currentuser", method: "get", headers: {…}, transformRequest: Array(1), transformResponse: Array(1), …}
data: ""
headers: {content-length: "0"}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"
__proto__: Object
type: "fetchuser"
__proto__: Object
我的数据在哪里丢失?