Redux发送index.html的内容



在测试动态创建的链接TO时,我的React应用程序发生了爆炸,现在redux正在返回index.html的内容。我以前见过一次,但忘记了如何修复它。当前的设置已经运行了几个月,所以我不想疯狂地更改一切。我通过检查'<!doctype html>'在调度之前,但随后的调度会成功,然后立即失败,还包含index.html的内容。我创建了一个新用户,同样的事情也发生了。我检查了数据库,清除了浏览器数据,检查了DevTools中的应用程序存储,并重置了我的计算机。真正奇怪的是,我在测试服务器上的版本也在做同样的事情,但它不会加载到另一台设备上。以下是我在React Developer Tools中看到的状态示例:

token:null
isAuthenticated:true
loading:false
user:"<!doctype html><html lang="en"><head><meta charset="utf-8"/> .."

如果不是我的电脑,那么比较这两个版本怎么会同时破坏它们呢?

编辑

加载用户操作

export const loadUser = () => async dispatch => {
if (localStorage.token) {
setAuthToken(localStorage.token);
}
try {
const res = await axios.get(`${API_PREFIX}/auth`);
dispatch({
type: USER_LOADED,
payload: res.data
});
} catch (error) {
dispatch({
type: AUTH_ERROR,
payload: error.message
});
}}

后端

router.get('/', auth, async (req, res) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch (err) {
console.error(err.message);
res.send(500).json({msg: 'User error'});
}
});

还原剂

const initialState = {
token: localStorage.getItem('token'),
isAuthenticated: null,
loading: true,
user: null,
online: null,
}
export default function(state = initialState, action) {
const { type, payload } = action;
switch(type){
case USER_LOADED:
return {
...state,
isAuthenticated: true,
loading: false,
user: payload
}
case REGISTER_SUCCESS:
case LOGIN_SUCCESS:
console.log(`nnnLOGIN PAYLOAD: ${JSON.stringify(payload)}`)
localStorage.setItem('token', payload.token);
return {
...state,
...payload,
isAuthenticated: true,
loading: false
}
case REGISTER_FAIL:
case AUTH_ERROR:
case LOGIN_FAIL:
case LOGOUT:
localStorage.removeItem('token');
return { 
...state,
token: null,
isAuthenticated: false,
loading: false
}
case ONLINE:
return{
...state,
online: payload
}
default:
return state;
}
}

您是否正在请求获取用户对象?我认为这只是获取一个网站(可能是你自己的(,而不是获取用户对象。

请分享您的代码以获取用户并将其存储到状态

事实证明,这个问题不是redux。我添加了app.get('/*'…catchall以防止在重新加载时获得"无法获取/路由",但它在我的路由之前,这意味着我的api调用返回了html.

之前(server.js(:

const root = require('path').join(__dirname, 'client', 'build');
app.use(express.static(root));
app.get('/*', (req, res) => {
res.sendFile('index.html', { root });
});
app.use(...);

之后:

app.use(...);
...
app.get('/*', (req, res) => {
res.sendFile('index.html', { root });
}

最新更新