0

I have a situation here. I have a ajax call which is used to query a value and return. in the Js file I gave like this

function getLoginData(){
    debugger;
    var getUserName=$("#inputUserName").val();
    $.ajax({
        url:'/users/loginValidation',
        dataType:'json',
        data:{"uName":getUserName},
        type:'post',
        async:false,
        success:function(data){
            debugger;
        }
    })
} 

In the Users.js file in routes I gave like this

router.post('/loginValidation',function(req,res){
    var db=req.db;
    var getName=req.body.uName;
    db.collection("Users").find({"name":getName},{"type":{$in: [ "admin", "owner" ]}}).toArray(function(err,result){
        res.json(result);
    })

})
  1. My requirement is I want to check in Users collection containing the name as in "getName" variable
  2. Along With that i want to check whether that name is of type "admin" or "owner".
  3. If both these conditions don't satisfy then return a error message

My user Collection is like this

{ "_id" : 1, "name" : "Rohith", "type" : "admin" }
{ "_id" : 2, "name" : "Kumar", "type" : "owner" }
{ "_id" : 3, "name" : "Krishna", "type" : "Sales" }
{ "_id" : 4, "name" : "Nikhil", "type" : "Sales" }
{ "_id" : 5, "name" : "Don", "type" : "admin" }

Now i am getting a null value in success of ajax call... Thanks

1 Answer 1

1

Use $and, like this:

db.collection("Users")
    .find({"$and": [
        {"name": getName},
        {"type": {"$in": [ "admin", "owner" ]}
    ]})
    .toArray(function(err,result){
        if (!err && !result) err = 'No results';
        if (err) return res.send(500, err);
        res.json(result);
    })
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot #3 of criteria, so added it to my answer. This will throw a HTTP 500 error for no results or a database error.

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.