我正在验证用户输入并生成 express,如果输入无效则返回 422,如果结果为空则返回 400。问题是我无法在发生错误时记录响应对象。
Nodejs:
if (q === '' || q.length < 3 || q.length > 150) {
console.log('invalid query');
return res.sendStatus(422).send('Search not found');
} else {
try {
// Fetch redis data
const data = await GET_ASYNC('data');
// stuff here
// Result Validation
if (!Array.isArray(data) || !data.length) {
res.status(400).send('Search not found');
} else {
// do stuff
res.status(200).send(data);
}
} catch (err) {
console.error(err);
res.sendStatus(500); // Server error
}
现在我的反应代码:
const searchDb = useCallback(async() => {
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
try {
axios.get(`/api/bestProduct?q=${searchValue}`,
{ cancelToken: source.token })
.then(res => {
console.log(res) // nothing shows up
const data = res.data;
setData(data)
});
} catch (err) {
if (axios.isCancel(err)) {
console.log(err.response); // nothing shows up
} else {
console.log('hello??') // nothing
return setError(`There's been a problem on our end.`)
}
}
}, [])
我查看了其他解决方案并尝试记录 res 和 res.status 但没有任何显示。这是我的控制台在错误期间的样子: