check if a user already exists in database node js using couchbase

448 Views Asked by At

I am trying to check if a user already exists in the database, I have managed to stop creating a user if one already exists with the same phone number , however I do not seem to get the error message displayed. I am not too sure why my error is not being handled correctly. Here is my code:

exports.usercreate = function (req, res)
{
    users.create(req.body, function (err, result)
    {
        var phonenumber = req.body.phonenumber;
        console.log(phonenumber);
        if (phonenumber.length > 0)
        {
            res.status(200).json(
            {
                status: "error",
                resCode: 400,
                msg: 'cutomer added error'
            });
        }
        else
        {
            res.status(200).json(
            {
                status: "success",
                resCode: 200,
                msg: "users Added Successfully",

            });
        }
        else
        {
            console.log(error)
        }
    });
};

Getting error like customer added error. but records are inserted in couchbase

1

There are 1 best solutions below

3
On

as @TommyBs mentioned, you are basically comparing an N1qlQuery object to whatever is coming on req.body.phonenumber

...
bucket.query(query, function(err, rows, meta) {
    for (row in rows) {
      if(row.phonenumber == req.body.phonenumber) {
        res.status(500).json({status:"error", resCode: 500, msg:"users Already exist"});

      }
    }
}