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