docs.length不适用于布尔值

时间:2019-10-17 10:07:46

标签: javascript node.js mongodb mongoose

在条件中传递true / false时,我的长度不起作用

如果要从数据库中找到true,我想返回一个已经存在的错误。

以下是我要实现的目标的代码

userModel.find({ is_reqestor: userObj.is_reqestor , is_deleted: false }, function (err, docs) {

    if (docs.length && userObj.is_deleted === false) {
         result.error = "Requestor already exists: " + userObj.is_reqestor;
         console.log("Requestor already exists", null);
         let resp = api_respose.getSuccessResponse(process.env.WEB_URI, result);
         resolve(resp);
    }
})

下面是我的userObj

{ email: 'esample@example.com',
  job_title: '',
  department: 'Credit',
  company_heading: 'Example',
  suite: '',
  city: 'D',
  state: 'CA',
  is_reqestor: true,
  is_active: true,
  name: 'Joe doe',
  role: '343434',
  legacy_id: '1323',
  __v: 0 }

1 个答案:

答案 0 :(得分:0)

因此,您不会在docs.length中获得布尔数据类型,因此您需要像这样检查它

if (docs.length > 0 && userObj.is_deleted === false)