0

I am working on an Node JS with Express API. After getting data from MySQL database, the response data must be pass in the variable outside the function.

Here is the primary function:

function get_orders(callback) {
  dbConnection.query(
    "CAll orders.getOrders()",
    async (err, rows, fields) => {
      if (!err) {
        if (rows.length !== 0) {
          return await callback(rows[0][0]);
        }
      } else {
        throw err;
      }
    }
  );
}

Here were I call the primary function:

function call_orders(req, res) {
  let data;
  get_orders(async (result) => {
    data = await result;
  });
}

I was assuming that data variable will have the values from get_orders() function but unfortunately its shows 'undefined'.

  • You can try using the async-await with NodeJS mysql library as shown in this Stack Overflow post (it works well): [node.js async/await using with MySQL](https://stackoverflow.com/questions/44004418/node-js-async-await-using-with-mysql). – prasad_ Mar 08 '23 at 07:13
  • Thanks for the information sir but my only problem is that the 'result' parameter can show the data but cannot transfer to the 'data' variable. It only displays 'undefined' when in console.log – Shirou Mochizuki Mar 08 '23 at 07:40
  • It is not very clear why you are mixing callbacks and async-await constructs. You can understand clearly if you use one or the other - it is possible. What do you want to do with the `data` variable after the assigning the value to it? – prasad_ Mar 08 '23 at 07:59
  • I will use the `data` variable as parameter for another function to call another query sir. – Shirou Mochizuki Mar 08 '23 at 08:20
  • I guess you need to clearly understand how callbacks, promises and async-await work - what is their purpose, etc. You can also refer this Stack Overflow post: [Can I mix callbacks and async/await patterns in NodeJS?](https://stackoverflow.com/questions/62037166/can-i-mix-callbacks-and-async-await-patterns-in-nodejs). – prasad_ Mar 08 '23 at 08:21
  • _"I will use the data variable as parameter for another function to call another query"_ How? You have not included that part in your question post. – prasad_ Mar 08 '23 at 08:23
  • Because when I tried to code like this: ``` function call_orders(req, res) { let data; get_orders( (result) => { data = "hello"; }); } ``` The `data` variable also gives 'undefined'. And so, what I think is that assigning value inside the function got the problem sir. – Shirou Mochizuki Mar 08 '23 at 08:36
  • Try using the second query from within the first query callback function (see this about nesting callbacks: https://stackoverflow.com/questions/64574300/understanding-nested-callback-execution-sequence ). – prasad_ Mar 08 '23 at 08:55
  • I'm sorry sir but I'm not using nested callback function and it makes my work messy sir. – Shirou Mochizuki Mar 08 '23 at 10:01
  • Application and program logic design matters to craft clear and manageable code. I cannot figure much from what is there. I can only suggest you search for your queries, specific topics and try explain your logic differently. I dont have further suggestions. – prasad_ Mar 08 '23 at 10:39

0 Answers0