1

Suppose, I have this:

connection.query("something", data, function(err, res) {
  console.log("inserted id is: " + res.insertId);
  obj123.method1("something", function(res2) {

    //how can I get access to res.insertId from here?
  })
});

My question is in the code. And the 2nd one, how would I get access to "more global" "res" if I named the argument "res" instead of "res2"?

Mario
  • 185
  • 9

2 Answers2

1

Most common method is just to create some temporary buffer which stores a reference to Your variable, like so:

connection.query("something", data, function(err, res) {
  var tempRes = res;
  console.log("inserted id is: " + res.insertId);
  obj123.method1("something", function(res) {
    tempRes.insertId();
  })
});
entio
  • 3,816
  • 1
  • 24
  • 39
  • and if I named them differently like I did in my example, I can very well access the 1st "res"? – Mario Mar 03 '16 at 12:35
  • Yes, then You can access both of them under respectively **res** and **res2** names – entio Mar 03 '16 at 12:41
1

you simply need to assign res to another variable

connection.query("something", data, function(err, res) {
  var anotherRes = res; //this line;
  console.log("inserted id is: " + res.insertId);
  obj123.method1("something", function(res) {

    console.log(anotherRes.insertId);
  })
});
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • There's really no need to create a second variable to refer to the one that was just named in the line above... Just change the function parameter's name. – Sébastien Vercammen Mar 03 '16 at 12:35
  • and if I named them differently like I did in my example, I can very well access the 1st "res"? – Mario Mar 03 '16 at 12:36
  • @SébastienVercammen whats the harm in assigning outer res so that it can be accessed inside inner function since `res` name will be used inside the inner function as well. – gurvinder372 Mar 03 '16 at 12:40
  • @gurvinder372 The outer res is already assigned in the function's definition, that's my point. There's no need to make another one. – Sébastien Vercammen Mar 03 '16 at 12:41
  • @Mario Variables are function scoped so inside the inner function res will have a new scope and its outer value won't be accessible inside inner scope unless you save it. – gurvinder372 Mar 03 '16 at 12:41
  • @gurvinder372 Following your definition, this should not work: https://jsfiddle.net/tvhgcqaj/. If you meant it specifically when naming both variables `res`, then yes, that was exactly my point - don't call both of them `res`. – Sébastien Vercammen Mar 03 '16 at 12:47
  • @SébastienVercammen this is working because inside inner function you are not defining new `res`. Try doing that and let me know if it worked. – gurvinder372 Mar 03 '16 at 12:48
  • @gurvinder372 You are obviously not reading or understanding what I said. – Sébastien Vercammen Mar 03 '16 at 12:49
  • **@Mario Variables are function scoped so inside the inner function res will have a new scope and its outer value won't be accessible inside inner scope unless you save it** -- I didn't ask what they functions were. I asked a different question. – Mario Mar 03 '16 at 15:59