1.我正在开发后端 API,但有时我需要从另一个 API 获取用户数据。我正在尝试使用 Axios 发出 http 请求来做到这一点。该请求按预期在浏览器中返回结果,但问题是我无法在终端中显示控制台日志。即使我要求程序这样做,它也没有显示任何内容。我的代码可能有问题吗?
2.错误消息=>>> POST http://localhost:8000/api/register 400(错误请求)错误:请求失败,状态代码为 400`
const handleSubmit = async () => {
//e.preventDefault();
try
{
// console.log(name, email, password, secret);
const { data } = await axios.post("http://localhost:8000/api/register", {
name,
email,
password,
secret,
});
setOk(data.ok); //useState component
}
catch (error) {
**strong text**
console.log(error.response.data);
}
}
从“../models/user”导入用户 //从'../helpers/auth'导入{ hashPassword,comparePassword }
export const register = async (req,res) => {
//console.log('Register endpoint =>', req.body)
//to make this work make express.json is applied in the above middleware
//console.log error to debug code
const {name, email, password, secret} = req.body;
//validation
if(!name) return res.status(400).send('Name is required')
if(!password || password.length < 6) return res.status(400).send('Password is
short
or password is not entered')
if(!secret) return res.status(400).send('Answer is required')
//The above code is for validation purpose to make sure data is correctly
entered
const exist = await User.findOne({email })
if(exist) return res.status(400).send('Email is taken')
}
.catch(error => {
console.log(error)
})
可能在你的 axios 上捕获错误是错误的,试试这个
只需在注册函数中尝试 console.log(req.body) 并查看终端中是否出现某些内容