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 6468017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:51:02+00:00 2026-05-25T05:51:02+00:00

I am writing a method to grab the lowest unique id in an ItemFileWriteStore.

  • 0

I am writing a method to grab the lowest unique id in an ItemFileWriteStore. I have a boolean to tell me when I reach my condition, but I can’t get the scope correct.

The function call works as I expect, apart from when newIdOkay is set to true, it is not recognised in the while loop.

Please can you explain why and what I have to do to get this right?

Many thanks

Here is my code:

function checkNewId( size ) {
    if( size == 0 ) {
        console.log('found new ID!');
        newIdOkay = true;
    }
 }

function addContentItem( store ) {    
    // New ID 
    var newIdOkay = false;
    var newId = 0;

    while( newIdOkay == false && newId < 8 ) {
        newId++;
        store.fetch({ query: {id:newId}, onBegin: dojo.hitch(this, "checkNewId"),                          
                      start:0, count:0, sync:true });
    }
} 
  • 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-05-25T05:51:03+00:00Added an answer on May 25, 2026 at 5:51 am

    To understand the reason, you need to have basic understanding of identifier resolution and scope chain in JavaScript. I recommend you to read this article: http://jibbering.com/faq/notes/closures/

    Regarding you question, in the function addContentItem, the check in the while loop is tested against the identifier newIdOkay which is in the of the activation object corresponding to this function execution context. In the function checkNewId, the identifier newIdOkay you set is in the global object. So these two are not the same one. An easy fix is to move the newIdOkay in addContentItem function to the global scope.

    var newIdOkay = false;
    
    function checkNewId( size ) {
        if( size == 0 ) {
            console.log('found new ID!');
            newIdOkay = true;
        }
     }
    
    function addContentItem( store ) {    
        // New ID 
    
        var newId = 0;
    
        while( newIdOkay == false && newId < 8 ) {
            newId++;
            store.fetch({ query: {id:newId}, onBegin: dojo.hitch(this, "checkNewId"),                          
                      start:0, count:0, sync:true });
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doubt about orkut's new URL Writing method. Earlier the home page
While writing a method in my app which uses playframework ,I need to get
I'm writing a method which approximates the Kolmogorov complexity of a String by following
I'm writing a method that will resize the columns in a datagridview so that
I'm writing a method to wrap another method as part of a class. It
Validating Primitive Arguments and Complex Data Validating Arguments When writing a method, arguments should
I wonder if there is a way of writing a method or a class
I want to write clean code. So When writing a method I want to
I'm using StreamWriter to write log file. And this is the writing log method,
We always alloc before init in Objective C, then while writing init method, Why

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.