1

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(){
      ...
   }      
}

1 Answer 1

3
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);
Sign up to request clarification or add additional context in comments.

5 Comments

@raynos, thanks a lot. I used the alternative format, it works perfectly. That is not an easy one. Cheers.
@Luc the only way I can imagine this bug being is if you had say 3 types, and had 3 row events for the last type, and zero row events for the first two.
@raynos, I have inner function that I did not yet modify with the alternative format. I will do this and let you know. I think it will be ok then :)
@raynos, because I did not set the inner functions correctly, I think the DB has been corrupted. I'll start again with an empty DB. I guess you're right. Thanks for your answer. Cheers, Luc
@raynos. Everything is fine now. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.