当我通过 isomorphic-fetch 模块调用 API 时,看起来它没有获得会话
When I call API by isomorphic-fetch module, looks like it doesn't get session
我正在使用 MERN 堆栈结构和 Redux,但同构获取模块有问题。
我想从会话中获取用户信息,但是 isomorphic-fetch 模块似乎它的请求与用户会话是分开的。
以下是视图调度一个操作以从会话中获取用户信息。
MainView.jsx:
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import * as Actions from '../../redux/actions/actions';
class MainView extends React.Component {
constructor(props) {
super(props);
this.displayName = 'MainView';
}
componentWillMount() {
if (this.props.showMessageModal.message !== '') {
this.props.dispatch(Actions.showMessageModal());
}
this.props.dispatch(Actions.fetchUserSession());
}
render() {
const renderTemp = () => {
if (this.props.user) {
return <div>{ JSON.stringify(this.props.user) }</div>;
}
};
return (
<div>
MainView
{ renderTemp() }
</div>
);
}
}
MainView.contextTypes = {
router: React.PropTypes.object,
};
function mapStateToProps(store) {
return {
showMessageModal: store.showMessageModal,
user: store.user,
};
}
MainView.propTypes = {
showMessageModal: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
user: PropTypes.object,
};
export default connect(mapStateToProps)(MainView);
以下是 action、reducer 和 router。
actions.js(仅相关代码):
import * as ActionTypes from '../constants/constants';
import Config from '../../../server/config';
import fetch from 'isomorphic-fetch';
const baseURL = typeof window === 'undefined' ? process.env.BASE_URL || (`http://localhost:${Config.port}`) : '';
export function getUserSession(user) {
return {
type: ActionTypes.GET_USER_SESSION,
user,
};
}
export function fetchUserSession() {
return (dispatch) => {
return fetch(`${baseURL}/api/session-user`)
.then((response) => response.json())
.then((response) => dispatch(getUserSession(response.user)));
};
}
reducer_user.js(在索引减速器中组合):
import * as ActionTypes from '../constants/constants';
export const user = (state = null, action) => {
switch (action.type) {
case ActionTypes.GET_USER_SESSION :
return action.user;
default:
return state;
}
};
user.router.js(api 路由器):
import { Router } from 'express';
router.get('/api/session-user', (req, res) => {
console.log('req.user: ' + req.user);
res.json(req.user);
});
export default router;
登录后,当我直接在浏览器上访问'/api/session-user'时,我可以看到这样的用户信息。
但是当我加载 MainView 并调度操作时,'req.user' in user.router.js returns 'undefined'。
请猜猜错在哪里。这会很有帮助。
我找到问题了。 'isomorphic-fetch' 没有设置请求 cookie,因此它无法从 cookie 获取会话。我已经在 action.js 中更改了我的代码,如下所示,它运行良好。
export function fetchUserSession() {
return (dispatch) => {
return $.ajax({
url: `${baseURL}/api/session-user`,
success: (response) => {
dispatch(getUserSession(response));
},
});
};
我参考了这个页面 - https://github.com/matthew-andrews/isomorphic-fetch/issues/75
你只需要添加credentials
参数:
export function fetchUserSession() {
return (dispatch) => {
return fetch(`${baseURL}/api/session-user`, {credentials: 'same-origin'})
.then((response) => response.json())
.then((response) => dispatch(getUserSession(response.user)));
};
}
我正在使用 MERN 堆栈结构和 Redux,但同构获取模块有问题。
我想从会话中获取用户信息,但是 isomorphic-fetch 模块似乎它的请求与用户会话是分开的。
以下是视图调度一个操作以从会话中获取用户信息。
MainView.jsx:
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import * as Actions from '../../redux/actions/actions';
class MainView extends React.Component {
constructor(props) {
super(props);
this.displayName = 'MainView';
}
componentWillMount() {
if (this.props.showMessageModal.message !== '') {
this.props.dispatch(Actions.showMessageModal());
}
this.props.dispatch(Actions.fetchUserSession());
}
render() {
const renderTemp = () => {
if (this.props.user) {
return <div>{ JSON.stringify(this.props.user) }</div>;
}
};
return (
<div>
MainView
{ renderTemp() }
</div>
);
}
}
MainView.contextTypes = {
router: React.PropTypes.object,
};
function mapStateToProps(store) {
return {
showMessageModal: store.showMessageModal,
user: store.user,
};
}
MainView.propTypes = {
showMessageModal: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
user: PropTypes.object,
};
export default connect(mapStateToProps)(MainView);
以下是 action、reducer 和 router。
actions.js(仅相关代码):
import * as ActionTypes from '../constants/constants';
import Config from '../../../server/config';
import fetch from 'isomorphic-fetch';
const baseURL = typeof window === 'undefined' ? process.env.BASE_URL || (`http://localhost:${Config.port}`) : '';
export function getUserSession(user) {
return {
type: ActionTypes.GET_USER_SESSION,
user,
};
}
export function fetchUserSession() {
return (dispatch) => {
return fetch(`${baseURL}/api/session-user`)
.then((response) => response.json())
.then((response) => dispatch(getUserSession(response.user)));
};
}
reducer_user.js(在索引减速器中组合):
import * as ActionTypes from '../constants/constants';
export const user = (state = null, action) => {
switch (action.type) {
case ActionTypes.GET_USER_SESSION :
return action.user;
default:
return state;
}
};
user.router.js(api 路由器):
import { Router } from 'express';
router.get('/api/session-user', (req, res) => {
console.log('req.user: ' + req.user);
res.json(req.user);
});
export default router;
登录后,当我直接在浏览器上访问'/api/session-user'时,我可以看到这样的用户信息。
但是当我加载 MainView 并调度操作时,'req.user' in user.router.js returns 'undefined'。
请猜猜错在哪里。这会很有帮助。
我找到问题了。 'isomorphic-fetch' 没有设置请求 cookie,因此它无法从 cookie 获取会话。我已经在 action.js 中更改了我的代码,如下所示,它运行良好。
export function fetchUserSession() {
return (dispatch) => {
return $.ajax({
url: `${baseURL}/api/session-user`,
success: (response) => {
dispatch(getUserSession(response));
},
});
};
我参考了这个页面 - https://github.com/matthew-andrews/isomorphic-fetch/issues/75
你只需要添加credentials
参数:
export function fetchUserSession() {
return (dispatch) => {
return fetch(`${baseURL}/api/session-user`, {credentials: 'same-origin'})
.then((response) => response.json())
.then((response) => dispatch(getUserSession(response.user)));
};
}