当我使用axios请求时,在打印到控制台时会得到预期的videoData
,但是由于某种原因我击中了catch语句。看来我的路由已正确设置,我的整个api路由正在执行并保存到数据库中,但我的catch语句正在执行,并出现控制台画面错误,如下所示
Axios请求
axios
.post("/api/videos/upload", videoData)
.then(res => {
dispatch({
type: ADD_VIDEO,
payload: res.data
});
//res.json({ msg: "Success" });
})
.catch(err => {
console.log("RESPONSE" + err.response.data);
dispatch({
type: GET_ERRORS,
payload: err.response.data
});
});
API路由
router.post(
"/upload",
passport.authenticate("jwt", { session: false }),
(req, res) => {
console.log(req.body);
const { errors, isValid } = validateUpload(req.body);
//Check Validation
if (!isValid) {
console.log("in videos!!");
//Return any errors with 400 status
return res.status(400).json(errors);
}
console.log("SCHEMA BEFORE");
const newVideo = new Video({
user: req.body.user,
description: req.body.description,
key: req.body.key,
thumbnail: req.body.thumbnail,
owner: req.body.owner
});
console.log("SCHEMA AFTER");
newVideo
.save()
.then(video => {
console.log("SAVED?");
res.json(video);
})
.catch(err => {
console.log("COULDNT SAVE");
res.json(err);
});
}
);