【发布时间】:2019-01-06 06:45:49
【问题描述】:
Axios Post 请求
// Create profile
export const createProfile = (profileData, avatar, history) => dispatch => {
dispatch(clearErrors());
const image = new FormData();
image.append("avatar", avatar, avatar.name);
axios
.post("/api/profile", image, profileData)
.then(res => history.push("/dashboard"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
编辑 ---> Axios 发布请求第二次尝试
// Create profile
export const createProfile = (profileData, avatar, history) => dispatch => {
dispatch(clearErrors());
const image = new FormData();
image.append("avatar", avatar, avatar.name);
image.append("user", profileData, profileData.username);
axios
.post("/api/profile", image)
.then(res => history.push("/dashboard"))
.catch(err =>
dispatch({
type: GET_ERRORS,
payload: err.response.data
})
);
};
profileData 是我在req.body 中想要的,头像是我在后端使用multer 在req.file 中收到的,但我收到的是带有图像的req.file,但我的@ 中没有987654327@(只是一个空对象)
这是我在节点中的路由器
router.post(
"/",
upload.single("avatar"),
passport.authenticate("jwt", { session: false }),
(req, res) => {
console.log(req.body);
}
);
【问题讨论】:
标签: node.js reactjs axios multer