-1

So I am currently working on a basic prototype and been running into some errors. I have a function that adds a user into the database. Before doing so, I made it read and execute a function to check the highest value of id, and then add it into the next query. But when I try to do this my query to add a user to the database is always executed before I get the value back from the database.

After doing some research, I found I will probably have to use async functions. I have been trying to find an answer that can suite my problem but can't seem to find an example that could work for me, I have seem many ways to write them out.

My code is this

async function maxIDD(maxID){
  try{
  connection.query('SELECT MAX(id) as maxID FROM users', (err,rows) => {
  var maxID = rows[0].maxID;
  console.log(maxID);
})
}
  catch (error) {
    console.log(error);
  }
  return maxID;
  }

async function addData(){
  const maxID2 = await maxIDD(maxID) + 1
  console.log('macidvalue = ' + maxID2)
  let sql = "INSERT INTO users (name, num, id, quantity) VALUES('"+ dataObject.userName +"','"+ dataObject.userNum +"','"+ maxID2 +"','"+ dataObject.userQuantity + "')";
  console.log(sql);
  connection.query(sql);

The first function maxIDD() is the one where I run the query to check the max id, then addData() is to add the user to the database.

This is where I am at currently, I am trying, to follow this answer "https://stackoverflow.com/questions/50751048/node-js-waiting-for-variable-to-be-set?answertab=active#tab-top"

Yet when I try and return my value (in this case maxID) It just get saved as "macidvalue = [object Promise]"

Not sure how I could solve this. All I need to do is query the database, and use that value somewhere else. Does anyone have any recommendations or suggestions?

john Doe
  • 1
  • 1
  • Whenever you see `[object Object]` (or similar) it's because your code has attempted to coerce an object into a string. Instead of `console.log('macidvalue = ' + maxID2)` use `console.log('macidvalue = ', maxID2)` (to see the object, won't fix the issue ofc) – freedomn-m May 11 '21 at 10:35
  • 3
    an `async` function will _always_ return a `Promise` object. That's what they do. – Alnitak May 11 '21 at 10:35
  • `const maxID2 = (await maxIDD(maxID)) + 1;` – trincot May 11 '21 at 10:36
  • @trincot the extra parens aren't needed - `await` has higher precedence than the binary `+` operator – Alnitak May 11 '21 at 10:43
  • @MuhammadOvi I have? I understand my question isn't the best and is noobish, but so is your answer. I have read the usage and still don't understand how to use it as I have tried what I think would work. If you don't know that's fine, I don't know ether! – john Doe May 11 '21 at 10:45
  • @trincot Yeah that will be for when I need to add one one, for now I am just trying to read the value in another function, thanks for the help though! – john Doe May 11 '21 at 10:46
  • @freedomn-m Gottcha, interesting, this will come in usefull in the future to check my values! – john Doe May 11 '21 at 10:47
  • @johnDoe, I don't understand this reaction. Your question says you get the output `macidvalue = [object Promise]`, which is happening in the line below the one that needs correction. – trincot May 11 '21 at 10:48
  • It's impossible for the code `const maxID2 = await maxIDD(maxID) + 1` to log a promise object. However, do note that your `maxIDD` function will only ever resolve to the parameter you've passed in, since the callback you give to `.query` is not going to return anything. – VLAZ May 11 '21 at 10:56

1 Answers1

1

You need to await the return value, so that code execution appears to pause at that point, continuing automatically when the result is returned and the Promise "resolved".

const maxID2 = await maxIDD(maxID) + 1

The return value of the await operator is the resolved value of the Promise generated by the async function.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Sorry I did not add that to my code but it still did not work, that was what I tried first, I will edit my code (I have been changing iit to try new things) I thought that would work, but unfortunately not. I will edit the question whenever I can find a working solution – john Doe May 11 '21 at 10:48
  • 1
    @johnDoe please don't edit your question in such a way that it changes the context. If you tried the above given the original question, it _should_ work. – Alnitak May 11 '21 at 10:48
  • ah, that said, your `maxIDD` function is also incorrect, because you're returning `maxID` outside of the database function's callback. If possible, use the async API to the database, not the callback based one - the two don't mix well unless you really know what you're doing. – Alnitak May 11 '21 at 10:55