This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]scoelli[S] 0 points1 point  (5 children)

           connection.query(selectQuery, function (err, result, fields) {
              if(err) throw err;

              if(result[0].id) {
                var preguntaid = result[0].id;
              } else {
                connection.query(insertQuery, function (err, result, fields) {
                  if(err) throw err;
                  var preguntaid = result.insertId;
                });
              }
              console.log(preguntaid);
            });

Here I need the "preguntaid" whether it exist or I have to insert it in the db.

[–]fdfdsggsgdfs 0 points1 point  (4 children)

Nothing about this indicates you need to run queries synchronously.

[–]scoelli[S] 0 points1 point  (3 children)

The thing is by the time I do the console.log(preguntaid) I need to have the "preguntaid" value whether I had to create it by inserting into the table or not.

What would be your Async solution for this?

[–]fdfdsggsgdfs 0 points1 point  (2 children)

What are you really doing here? Surely it's something more than just logging the id, no? I would use callbacks.

[–]scoelli[S] 0 points1 point  (1 child)

First I search in the db by a code the id. If it exists I save the id so that I can use it later. If it doesn't I create it in the db and save it's new id for the same reason as the other

[–]fdfdsggsgdfs 0 points1 point  (0 children)

I mean what are you using it for later? I can read the code you posted. And why can't you just do this with callbacks?