2

I am trying to find whether my collection has a record with profilename = john and if exists I return status success if else I return fail but in my case, it is returning success for both cases.I am new to node and mongo can any one help me.

My function,

    exports.searchprofilename = function (req, res) {
  var params = req.params;console.log(req.params.id);
  var record= db.collection('profile');
   record.find({profilename:params.id}, (err, result) => {
   if (err){ return console.log(err)
    }
      if(!result){
            data = {status:'success'};
        } else{
             data = {status:'profile name already exists'};
        }
      res.send(data);
  });
};
0

6 Answers 6

1

If you are only checking if a record exists, you should be easily able to do it using db.collection.count() method and checking if the number of records = 0 or not.

https://docs.mongodb.com/manual/reference/method/db.collection.count/

Honestly, I am way new to mongodb and I still cannot grasp the idea of cursors which is the return type of db.collection.find() as per https://docs.mongodb.com/manual/reference/method/db.collection.find/

Sign up to request clarification or add additional context in comments.

Comments

1

I cleared it by changing find({}) to findOne({}),Thank you every one.

Comments

0

If your query matches then it means you have record then return Success

 exports.searchprofilename = function (req, res) {
  var params = req.params;console.log(req.params.id);
  var record= db.collection('profile');
   record.find({profilename:params.id}, (err, result) => {
   if (err){ return console.log(err)
    }
    // If record exist then return 'Success'
      if(result.length>0){
            data = {status:'success'};
        } else{
             data = {status:'profile name already exists'};
        }
      res.send(data);
  });
};

1 Comment

That's what I thought too. But the documentation wasn't very clear about properties of Cursors. Thanks for explaining.
0

I think in your case, req.params.id is a String for example '123', but in your mongodb profilename field is stored as an Number.

So you can try this:

change {profilename:params.id} to {profilename:parseInt(params.id)}

Comments

0

Try this

    exports.searchprofilename = function (req, res) {
        console.log("PARAMS",req.params.id);
        var data = {};
        profile.findOne( {profilename:req.params.id} , function (err, fetchDataObj) {
                if (err) {
                    console.log(err)
                   return err;
                } else {
                    data.status = 'success';
                    data.result = fetchDataObj;
                    return data;
                }
            }).lean(true)

  });

1 Comment

@downvoter please identify whats wrong in the answer??
-1

Try to Debug. the type of result is array, so try to check the length of it:

if(result.length==0){
    data = {status:'success'};
} else{
    data = {status:'profile name already exists'};
}

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.