Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:39:13+00:00 2026-06-09T03:39:13+00:00

I commented the values on the first (and only) execution of my for loop.

  • 0

I commented the values on the first (and only) execution of my for loop. It doesn’t seem to matter what I do… after connection.query my for loop is always given a value that escapes the loop. No idea why. I can provide more examples. Anyone know what might be going on and how I can get around the problem?
Here is a snippet of code that has this problem.
The mysql plugin for node.js i’m using is here

https://github.com/felixge/node-mysql

for (j=0;j<rows[i].movelimit;j++){
            //j=0
               sql2="SELECT x, y, land FROM map WHERE collision = 0 AND x>"+(lastX-55)+" AND x<"+(lastX+55)+" AND y>"+(lastY-55)+" AND y<"+(lastY+55)+" AND land='"+rows[i].land+"'"+restrictions;
               connection.query(sql2, function(err, move, fields) {
            //j=rows[i]movelimit 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:39:14+00:00Added an answer on June 9, 2026 at 3:39 am

    It seems to be an asynchronous callback. Therefore, your loop variable j which is out of the scope of the callback function, will always evaluate to the last value it got assigned before the loop ended. See also javascript for loop unexpected behaviour or JavaScript closure inside loops – simple practical example.

    Use a closure:

    function getCallbackFn(index) {
        return function(err, move, fields) {
            ....
            // "index" will always point to the function argument
        };
    }
    for (j=0;j<rows[i].movelimit;j++) {
        var sql2 = ...
        connection.query(sql2, getCallbackFn(j));
    }
    

    Shorter, inline IEFE:

    for (j=0;j<rows[i].movelimit;j++) (function(j){
        var sql2 = ...
        connection.query(sql2, function(err, move, fields) {
            ....
            // "j" points to the function argument
        });
    })(j);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After saving some values into the database, I am finding it difficult to print
In this thread some one commented that the following code should only be used
How do I change the connection string in a code first entity framework/MVC application?
Below I have 3 prototypes. I expected the first one (commented out) to work,
Can anyone please tell/suggest me how can i store and retrieve comments multiple/different values
I saw comment If you have 50 million values between 10 and 15 characters
I cant find one way to get this value (comment) into json using javascript.
I wanted to create a default value for :order : class Comment < ActiveRecord::Base
I commented my code below to reflect what I am attempting to do here.
I have commented out the hi Fold … line in my current vim-colorscheme (xoria256

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.