【问题标题】:check if a user already exists in database node js using couchbase使用 couchbase 检查用户是否已存在于数据库节点 js 中
【发布时间】:2019-10-16 06:41:04
【问题描述】:

我正在尝试检查数据库中是否已经存在用户,如果已经存在具有相同电话号码的用户,我已经设法停止创建用户,但是我似乎没有显示错误消息。我不太清楚为什么我的错误没有得到正确处理。这是我的代码:

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

出现错误,例如客户添加的错误。但是记录被插入到沙发库中

【问题讨论】:

  • 我不熟悉节点中的 couchbase,但据我从文档中可以看出,您从未真正调用您的查询 docs.couchbase.com/nodejs-sdk/current/… 并且没有回调来进行比较。我建议查看我链接的文档并重写您的代码以实际调用您的查询并使用适当的回调
  • 你在用脚凳吗?
  • 是的,我正在使用脚凳@MatthewGroves

标签: node.js couchbase n1ql couchbase-ottoman


【解决方案1】:

正如@TommyBs 提到的,您基本上是在将 N1qlQuery 对象与 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"});

      }
    }
}

【讨论】:

  • 我已更改,但出现另一个错误 TypeError: Cannot read property 'query' of undefined
  • 您的问题与基本的 NodeJs 语法有关,您可以编辑您的问题并添加代码的新部分吗?
  • 我已经更改了我的代码@deniswsrosa 请检查一下
猜你喜欢
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-17
  • 2021-12-27
  • 2013-08-31
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多