简体   繁体   中英

node.js / postgres… asynchronous problem?

I have a list of types: ["type1", "type2, "type3"], on which I loop. For each type:
- create / execute a query to get the items of a the current type
- run another function when the query if done ( on('end') ).

 for ( var i=0; i<types.length; i++ ){
    type = types[i];
    value = "test";
    ...

    // ADD ITEM: QUERY
    var query = client.query(...); // QUERY ITEMS OF A THE CURRENT TYPE

    // ADD ITEM: ERROR CHECKING
    query.on("error", function (err) {
      ...
    });

    with ({ t: type, v: value }) {  // I HAD TO DO THAT SO THAT ALL THE TYPES ARE TAKEN INTO ACCOUNT
      query.on('end', function() {
        my_function(t, v);
      });
    }
  }

my_function is like:

function my_function(type, value){
   console.log(type + ',' + value);  // CORRECT (I CAN SEE ALL THE TYPES BEEING LISTED)

   // QUERY OTHER STUFF BASED ON THE VALUE OF "type"
   var query = client.query(...);

   // MAIN STUFF
   query.on('row', function(row){
      console.log(type + ',' + value);  // THIS DOES NOT WORK ANYMORE... ONLY THE LAST TYPE IS TAKEN INTO ACCOUNT WHERE I EXPECT TO GET THIS MESSAGE FOR EACH TYPES.
     ...
   }

   // FINALIZE
   query.on('end', function(){
      ...
   }      
}

I guess this is linked to asynchronous process... but cannot figure out where the error is.

UPDATE

I have updated my loop so it looks like:

 for ( var i=0; i<types.length; i++ ){
    type = types[i];
    value = "test";
    ...

    // ADD ITEM: QUERY
    var query = client.query(...); // QUERY ITEMS OF A THE CURRENT TYPE

    // ADD ITEM: ERROR CHECKING
    query.on("error", function (err) {
      ...
    });

    // MAIN STUFF GOES HERE
    (function(t, v) { query.on("end", function() {
      console.log(t + ',' + v); // OK, I CAN SEE THIS DISPLAYED FOR EACH TYPE
      my_function(t, v);
    }); })(type, value);
  }

I modified my_function so it looks like:

function my_function(type, value){
   console.log(type + ',' + value);  // OK, I CAN SEE THIS DISPLAYED FOR EACH TYPE

   // QUERY OTHER STUFF BASED ON THE VALUE OF "type"
   var query = client.query(...);

   // MAIN STUFF
   (function(t, v) {
     query.on("row", function(row) {
       console.log('TEST:' + t + ',' + v); // KO, I CAN ONLY SEE THIS DISPLAYED FOR ONE TYPE
   }); })(type, value);

   // FINALIZE
   query.on('end', function(){
      ...
   }      
}
with ({ t: type, v: value }) {  // I HAD TO DO THAT SO THAT ALL THE TYPES ARE TAKEN INTO ACCOUNT
      query.on('end', function() {
        my_function(t, v);
      });
    }

Is broken. What you want is this

query.on("end", my_function.bind(null, type, value));

Function.prototype.bind allows you to bind parameters to a function.

Never use with . An alternative that also works would be:

(function(t, v) {
  query.on("end", function() { my_function(t, v); });
})(type, value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM