Axios没有捕获到“电子邮件重复条目”错误。我的axios函数在动作创建器(redux)中,该创建器将帖子发送到我的快速服务器。这是电话
axios.post(api + '/api/register', { user })
.then(res => {
dispatch(success(user));
dispatch({ type: ADD_USER, payload: res.data.data });
})
.catch(error => {
alert('error');
// dispatch({ type: ADD_TOAST, payload: error.response.data });
});
我没有收到警报,也没有回应(res)?但是在我的服务器上,我控制台记录了错误,向我显示了“错误:重复输入”消息。这是错误所独有的还是我的代码中存在错误?
**使用快速文件编辑
router.post('/', function(req, res) {
let firstname = req.body.user.firstname;
let lastname = req.body.user.lastname;
let email = req.body.user.email;
let username = req.body.user.username;
let password = req.body.user.password;
bcrypt.hash(password, 10, function(err, hash) {
if(err) {
console.log('Could not hash password: ' + err);
} else {
// insert into users with hash then profiles
database.query('INSERT INTO Users (ID, Firstname, Lastname, Email, Password, CreateDate) VALUES (NULL, ?, ?, ?, ?, CURRENT_TIMESTAMP)', [firstname, lastname, email, hash], function(err, result) {
if(err) {
console.log(err);
} else {
// get inserted id && insert into profiles
let userID = result.insertId;
database.query('INSERT INTO Profiles(ID, UserID, Photo, Bio) VALUES (NULL, ?, NULL, NULL)', [userID], function(err, result) {
if(err) {
console.log('We could not create your profile' + err);
} else {
let data = {
firstname: firstname,
lastname: lastname,
email: email,
photo: null
}
res.json({ data });
}
});
}
});
}
});
});
答案 0 :(得分:0)
您似乎必须使用
返回
axios函数中的声明
像这样:
async function register (data) {
return axios.post(api + '/api/register', data)
.then(res => {
dispatch(success(user));
dispatch({ type: ADD_USER, payload: res.data.data });
})
.catch(error => {
alert('error');
// dispatch({ type: ADD_TOAST, payload: error.response.data });
});
}
答案 1 :(得分:-1)
在catch中从console.log(error)修改为console.log(error.response)。
axios.post('/formulas/create', {
name: "",
parts: "" }).then(response => {
console.log(response) }) .catch(error => {console.log(error.response)});