0

In the code below I have an object list named foundUser. I insert data into it and at the very bottom of the code I print it in the console and it shows as empty. I have been trying to figure this out for hours now and I am unable to find a solution. Any help will be greatly greatly appreciated.

addUser: (req, res) => {
  const body = req.body;
  let errors = {};
  let foundUser = {};
  pool.query('SELECT email from registration WHERE email = ?', [body.email], (error, result) => {
    if (error) {
      console.log(error);
    }
    if (result.length > 0) {
      pool.query('SELECT id, name, email from registration WHERE email = ?', [body.email], (error, result) => {
        if (error) {
          console.log(error)
        } else {

          var toString = JSON.stringify(result)
          var toJSON = JSON.parse(toString)
          foundUser.id = toJSON[0].id
          console.log(foundUser);
          req.body.supplier_id = foundUser.id//The object value inserted in foundUser is not being assigned to req.body.supplier_id


        }
      })
    }
    console.log(foundUser) // In my console it shows as empty.
    //I want to then take the object inserted in founderUser and assign it to a field in mySQL but I cant since foundUser is empty
  })
}
ForestProgramming
  • 83
  • 1
  • 1
  • 10
  • 1
    `pool.query()` is asynchronous. Put the `console.log()` inside the callback function. – Barmar Oct 02 '20 at 23:22
  • Right I tried that and it worked but when I want to use the object inserted and assign the value to a field in mysql. But i cant since it shows as empty – ForestProgramming Oct 02 '20 at 23:31
  • 1
    Everything that depends on the result has to be in the callback function. – Barmar Oct 02 '20 at 23:34
  • Why are you doing two SELECT queries for the same row? – Barmar Oct 02 '20 at 23:35
  • 1
    If you want to insert the found result into another MySQL field, why don't you do that directly in the query? Use UPDATE + JOIN to update one table based on another table. – Barmar Oct 02 '20 at 23:37
  • See https://stackoverflow.com/questions/12394506/mysql-update-table-based-on-another-tables-value – Barmar Oct 02 '20 at 23:37
  • Okay I will try that. I updated my code with a comment so you can see what I am talking about – ForestProgramming Oct 02 '20 at 23:50
  • 1
    Just move what you want into the code block right after `foundUser.id = toJSON[0].id` – Barmar Oct 02 '20 at 23:53
  • 1
    Why are you calling `JSON.stringify` and `JSON.parse`? You can just use `result[0].id` – Barmar Oct 02 '20 at 23:53
  • Yep you were correct. That worked. Adding an additional query within another query was causing problems. This was the most efficient way to do it. Voted your answer to be correct. Thank you so much – ForestProgramming Oct 03 '20 at 04:43
  • Hey Barmar since in my code above I am searching by email from one table and inserting the data into another table - before inserting this data how can I write in the same query to check if data already exists before inserting? – ForestProgramming Oct 03 '20 at 16:42
  • If `email` is a unique key in the other table, you can use `INSERT IGNORE` and it will skip it if it already exists. You can also use `ON DUPLICATE KEY UPDATE` if you want to update it instead of inserting. – Barmar Oct 03 '20 at 17:32
  • So the data is being inserted in join_baseball_team request table where the user looks up another user with email and if they found it they send the request to join baseball team. Now if the request has been sent already I wanted to simply show a message that the request has been sent already. If not then they are allowed to insert into the join_baseball_team table – ForestProgramming Oct 03 '20 at 18:19
  • I was thinking of doing a subquery for that...Unless there is a more efficient way to do it – ForestProgramming Oct 03 '20 at 18:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222459/discussion-between-barmar-and-forestprogramming). – Barmar Oct 03 '20 at 18:31
  • We shouldn't design this here. You can use a SELECT query and return a message if it's already in there. If you have trouble getting it to work, post a new question. – Barmar Oct 03 '20 at 18:42

0 Answers0