0

I am trying to use the value of a variable in a mongo expression as you can see below:

DbModels.prototype.findByParam = function(_collection, param, id, callback) {
    this.getCollection(_collection, function(error, sel_collection) {
      if( error ) callback(error)
      else {
        sel_collection.findOne({param.toString(): id}, function(error, result) {
          if( error ) callback(error)
          else callback(null, result);
        });
      }
    });
};

However, the param.toString() gives an error. Any suggestions how I can use the value of the param variable in the mongoDb expression?

Thanks

1
  • What error are you getting? What is the value of param? Commented Feb 26, 2012 at 18:41

1 Answer 1

3

This syntax {param.toString(): id} is not valid.

Try

var query = {};
query[param.toString()] = id;
sel_collection.findOne(query, function(error, result) {
  if( error ) callback(error)
  else callback(null, result);
});
Sign up to request clarification or add additional context in comments.

Comments

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.