Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-13401

Javascript (2 issues): loop variable evaluation error

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.9
    • Component/s: Shell
    • ALL
    • Hide

      Script body:
      ==============================================

      db.t.remove();
      db.t.save({user:{fn:"Bob",ln:"Marley"},grades:["A", "C", 5, 1.4, {gr:"good"}],type:4});
      db.t.save({user:{fn:"Chuck",ln:"Norris"},grades:["F", "F", 3.5, 4, {gr:[1,2,3]}], type:6.3});
      db.t.save({user:{ln:"Washington"},grades:[{gr:[1,2,3]}]});
      db.t.save({user:{fn:"Hello",ln:"World"}});
      db.t.save({age:25, type:"offline"});
      db.t.save({age:26, type:"online"});
      db.t.save({age:27});
      db.t.save({age:28, type:"single"});
      db.t.save({age:40, type:"married"});
      
      var qs = [{type:{$gt:4}}]
      
      var i = 0;
      for (i = 0; i < qs.length; i++)
      {
        print("\nQuery: "+JSON.stringify(qs[i]));
        db.t.find(qs[i], {_id:0});
      }
      

      ==============================================

      When executing this script I get the following result:

      C:\Downloads\mongodb\bin>mongo -quiet test < test.js
      Fri Mar 28 10:53:22.512 SyntaxError: Unexpected end of input
      Fri Mar 28 10:53:22.514 ReferenceError: i is not defined
      

      If I change the for loop like this:

      var i = 0;
      for (i = 0; i < qs.length; i++)
      

      I get:

      C:\Downloads\mongodb\bin>mongo -quiet test < test.js
      Fri Mar 28 10:54:53.021 SyntaxError: Unexpected end of input
      
      Query: {"type":{"$gt":4}}
      { "user" : { "fn" : "Chuck", "ln" : "Norris" }, "grades" : [  "F",  "F",  3.5,  4,  {  "gr" : [  1,  2,  3 ] } ], "type"
       : 6.3 }
      

      So with this modification I get a valid result, but still see some strange SyntaxError.

      C:\Downloads\mongodb\bin>mongo -version
      MongoDB shell version: 2.4.9
      
      Show
      Script body: ============================================== db.t.remove(); db.t.save({user:{fn: "Bob" ,ln: "Marley" },grades:[ "A" , "C" , 5, 1.4, {gr: "good" }],type:4}); db.t.save({user:{fn: "Chuck" ,ln: "Norris" },grades:[ "F" , "F" , 3.5, 4, {gr:[1,2,3]}], type:6.3}); db.t.save({user:{ln: "Washington" },grades:[{gr:[1,2,3]}]}); db.t.save({user:{fn: "Hello" ,ln: "World" }}); db.t.save({age:25, type: "offline" }); db.t.save({age:26, type: "online" }); db.t.save({age:27}); db.t.save({age:28, type: "single" }); db.t.save({age:40, type: "married" }); var qs = [{type:{$gt:4}}] var i = 0; for (i = 0; i < qs.length; i++) { print( "\nQuery: " +JSON.stringify(qs[i])); db.t.find(qs[i], {_id:0}); } ============================================== When executing this script I get the following result: C:\Downloads\mongodb\bin>mongo -quiet test < test.js Fri Mar 28 10:53:22.512 SyntaxError: Unexpected end of input Fri Mar 28 10:53:22.514 ReferenceError: i is not defined If I change the for loop like this: var i = 0; for (i = 0; i < qs.length; i++) I get: C:\Downloads\mongodb\bin>mongo -quiet test < test.js Fri Mar 28 10:54:53.021 SyntaxError: Unexpected end of input Query: {"type":{"$gt":4}} { "user" : { "fn" : "Chuck", "ln" : "Norris" }, "grades" : [ "F", "F", 3.5, 4, { "gr" : [ 1, 2, 3 ] } ], "type" : 6.3 } So with this modification I get a valid result, but still see some strange SyntaxError. C:\Downloads\mongodb\bin>mongo -version MongoDB shell version: 2.4.9

      This bug is actually submitted for 2 issues:
      1) unrecognized variable declaration within the loop and
      2) irrelevant SyntaxError.

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            rafoid Raf Oid
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: