0

I want to use code to manipulate 'condition' as variable before passing into 'find' method (for MongoDB):

let condition;

if (cat) {
   condition = { $and: [{brand: brnd},
                        {category: cat} ] 
               }  
} else { 
   condition = {brand: brnd} 
}

productCol.find(condition);

But I am unable to get this to work. Can anyone help me how I can make it to work? Thank you

1
  • Correction to above. Above code actually is working other than my own mistake in value passing that give inaccurate result, with thanks to saketh on the hint below. Commented Feb 23, 2019 at 18:26

1 Answer 1

1

Can you be more specific about your code like how did u setup your connection and how are you trying to use the result and what errors are you getting when you are trying to run your code. Please try the below code.

    var FindQuery=function(cat){
    if (cat) {
    var condition = { $and: [{brand: brnd},
                    {category: cat} ] }  
    } else { 
    var condition = {brand: brnd} 
    }

    return productCol.find(condition);
    };
    FindQuery(condition)
Sign up to request clarification or add additional context in comments.

1 Comment

hi Saketh, I check that your code works (but code in different way). When I test again onto my code, I then realised is the value passing mistake that I make. It works also otherwise. Thank you

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.