0

ok this is driving me crazy..

function(amount){

matchingOrder = Order.findOne({
        Price: {
          $lte: amount
        }
      }, {
        sort: {
          Price: 1,
          Order_ID: 1
        }
      });

}

-----does not work

this works:

function(){

amount = 2

matchingOrder = Order.findOne({
        Price: {
          $lte: amount
        }
      }, {
        sort: {
          Price: 1,
          Order_ID: 1
        }
      });

}

in both cases console.log(amount) is 2 so variable gets passed

...sorry for maybe obvious scope or something..im relativly new at this

xan4fun
  • 31
  • 6
  • Define "does not work"? – Sterling Archer Mar 10 '14 at 14:45
  • How are you calling this? It is an anon function so maybe there is a problem in how you are assigning its parameter? – Sammaye Mar 10 '14 at 14:53
  • Maybe weird case of semicolon insertion? Try inserting semicolon after amount = 2 just to be sure...and use var while you are at it, if the variable is not defined with var somewhere else. – Capaj Mar 10 '14 at 14:53

2 Answers2

1

The only thing coming to my mind could be different types for amount. Try this instead:

function(amount){

matchingOrder = Order.findOne({
        Price: {
          $lte: parseInt(amount)
        }
      }, {
        sort: {
          Price: 1,
          Order_ID: 1
        }
      });

}
Emilio Rodriguez
  • 5,709
  • 4
  • 27
  • 32
  • It is better to check the type at the beginning, because like this if parseInt returns NaN and you execute the query, you waste your DB's time on useless query. – Capaj Mar 10 '14 at 15:01
0
function query_am(input){

 var amount = input; //pay attention, as pre-condition your input must be a number.

 matchingOrder = Order.findOne({
    Price: {
      $lte: amount
    }
  }, {
    sort: {
      Price: 1,
      Order_ID: 1
    }
  });

}

To invoke this function, for example: query_am(2) or query_am(6) . don't do query_am('6')

Morrisda
  • 1,380
  • 1
  • 11
  • 25
  • for checking if number is indeed a number, check out this spectacular answer: http://stackoverflow.com/a/1830844/671457 – Capaj Mar 10 '14 at 14:59