简体   繁体   English

错误处理:如何在我的前端接收来自我的后端的错误消息?

[英]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.

相关问题 如何在前端调用后端错误消息? - How to call backend error message on the frontend? 如何将错误消息从服务器后端传递到 vue 前端 - How can I pass an error message from the server backend to vue frontend 当我尝试从我的前端(React-Native/Mongoose)的后路由器发送一组 ObjectID 时,我的后端出错 - Error in my backend when I'm trying to send an array of ObjectID from a post router from my frontend (React-Native/Mongoose) 快速错误处理:向前端发送消息 - Express Error Handling: Sending a Message to Frontend 我在rss feed上收到错误消息,提示未找到数据 - I receive error message on my rss feed says no data was found NodeJs:如何从我的 HTML 前端访问我的 JavaScript 后端的功能? - NodeJs: How do I access the functions of my JavaScript backend from my HTML frontend? 将消息从后端传递到前端 - Passing message from backend to frontend 如何使用 API 在我的后端访问我的 angular 前端图像 - How to access to my angular frontend image in my backend with API 无法通过我的前端从我的节点 js 服务器下载图像文件(我的后端和前端是解耦的) - Failed to download an image file from my node js server through my frontend (my backend and frontend are decoupled) 在显示错误处理的错误消息后,后端正在将数据提交到数据库。 [节点+快递] - Backend is submitting data to database after displaying error message from error handling. [Nodejs + Express]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM