[英]Error handling: How to receive error message from my backend on my frontend?
I'm building my first full stack application, and I'm having problem with error handling on my backend, I also don't know how to receive this error message on my frontend.我正在构建我的第一个全栈应用程序,并且我在后端的错误处理方面遇到了问题,我也不知道如何在我的前端接收此错误消息。
Here is how my application is working: (I'm using nodejs, express.js, mongodb and react)这是我的应用程序的工作方式:(我正在使用 nodejs、express.js、mongodb 并做出反应)
// 1. I got a form where user provide some data.
// 2. Redux action creator is sending this data to my API.
// 3. Basing on this data, my API is fetching other API for some information.
// 4. And the response is pushed to my data base, and then send back to my reducer, and it's in my store.
Thing is, that before I push the response to my database I want to check if this item exists in the API that I'm fetching on the backend, and if not then I want to receive this kind of message, and display that to my user just below the form.问题是,在我将响应推送到我的数据库之前,我想检查这个项目是否存在于我在后端获取的 API 中,如果不存在,那么我想接收这种消息,并将其显示给我的表单下方的用户。
Here is my action creator on frontend:这是我在前端的动作创建者:
export const addTransaction = (transaction) => async (dispatch) => {
try {
const { data } = await api.addTransaction(transaction);
dispatch({
type: ADD_TRANSACTION,
payload: data
});
} catch (error) {
console.log(error.message);
};
};
And here is my backend controller:这是我的后端 controller:
export const addTransaction = async (req, res) => {
const transaction= req.body
const myUpdatedTransaction = {
...transaction,
someOther: "fields"
};
const requestOptions = {/*GET, url, api_key, etc. */},
// here is where I'm trying to handle this case
await request(requestOptions).then( async (resolve) => {
if(resolve) {
const newTransaction = new TransactionItem(myUpdatedTransaction)
await newTransaction.save();
res.status(201).json(newTransaction);
return;
}
}, (err) => {
/******** And here where I don't know how to deal with this *******/
console.log('error ----->');
if(err) {
res.status(409).json({ message: 'Check if the ticker is correct.'});
console.log('409');
return;
}
});
};
I will appreciate if someone can share some links where I can learn more about error handling.如果有人可以分享一些链接,我将不胜感激,我可以在其中了解有关错误处理的更多信息。
Well it looks like you're mixing up two concepts.好吧,看起来你混淆了两个概念。
https://www.smashingmagazine.com/2020/11/comparison-async-await-versus-then-catch/ https://www.smashingmagazine.com/2020/11/comparison-async-await-versus-then-catch/
export const addTransaction = async (req, res) => {
const transaction= req.body
const myUpdatedTransaction = {
...transaction,
someOther: "fields"
};
const requestOptions = {/*GET, url, api_key, etc. */},
// here is where I'm trying to handle this case
try{
const resolve = await request(requestOptions);
if(resolve) {
const newTransaction = new TransactionItem(myUpdatedTransaction)
await newTransaction.save();
res.status(201).json(newTransaction);
return;
}
}catch(err){
/******** And here where I don't know how to deal with this *******/
console.log('error ----->');
if(err) {
res.status(409).json({ message: 'Check if the ticker is correct.'});
console.log('409');
return;
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.