xan4fun
xan4fun

Reputation: 31

Variable into mongodb query

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

Upvotes: 0

Views: 400

Answers (2)

Morrisda
Morrisda

Reputation: 1420

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

Upvotes: 0

Emilio Rodriguez
Emilio Rodriguez

Reputation: 5749

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

}

Upvotes: 1

Related Questions