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

3
  • Define "does not work"? Commented Mar 10, 2014 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? Commented Mar 10, 2014 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. Commented Mar 10, 2014 at 14:53

2 Answers 2

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
        }
      });

}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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')

1 Comment

for checking if number is indeed a number, check out this spectacular answer: stackoverflow.com/a/1830844/671457

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.