简体   繁体   English

在保存到Mongoose中之前如何检查数据库中是否存在嵌入式文档

[英]How to check if an embedded document exists in database before saving in Mongoose

I'm making a basic authentication system using Express, Mongoose and PassportJS. 我正在使用Express,Mongoose和PassportJS构建基本的身份验证系统。 What I want to do is to check the database if the entered username and password already exist in the database. 我要做的是检查数据库,如果输入的用户名和密码已经存在于数据库中。 Here's my sample code below: 这是下面的示例代码:

//Post: /signup
app.post('/signup', function (req, res) {
  var username = req.body.person.user.username;
  var password = req.body.person.user.password;

  Person.user.find({'username': username}, function (err, user) {
    if (err) {
      console.log(err.name);
    } else {
      console.log('User Found');
    }
  });
});

The problem is it returns this kind of error: 问题是它返回这种错误:

TypeError: Cannot call method 'find' of undefined

Could someone please help me. 有人可以帮我吗。

As robertklep pointed, you are not checking correctly for an user's existence. 正如robertklep指出的那样,您没有正确检查用户的存在。 Also, since username is most likely unique, you can just use findOne (findOne() returns a single object while find() may also return a single object but will wrap it in an array). 另外,由于用户名很可能是唯一的,因此您可以使用findOne(findOne()返回单个对象,而find()也可能返回单个对象,但会将其包装在数组中)。

Person.findOne({'username': username}, function (err, user) {
  if (err) {
     console.log(err.name);
     return;
  }
  if (!user)
    console.log('User not Found');
    return;
  }
  console.log('User found');

});

I think you're looking for this: 我认为您正在寻找这个:

Person.find({ 'user.username' : username }, ...)

FWIW, if the callback isn't called with an error it doesn't mean a user was found, it just means that there were no errors performing the query. FWIW,如果没有错误地调用回调函数,则并不表示已找到用户,这仅意味着执行查询没有错误。 But user can still be null, meaning that the query didn't have any matching results. 但是user仍然可以为null,这意味着查询没有任何匹配结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用Mongoose保存之前创建文档的副本? - How to create a copy of a document before saving with Mongoose? 即使我检查文档是否已存在,Mongoose仍在保存文档 - Mongoose is saving documents even if I check to see if the document already exists 如何检查 mongoose 文档中是否存在密钥? - How to check if key exists on mongoose document? 如何在不检索的情况下检查文档是否存在:猫鼬 - How to check if a document exists without retrieving it: Mongoose 在将输入保存到数据库之前如何检查密码是否与确认密码相同(mongoose + express + validator) - How can I check if password is the same as confirmed password before saving the input into database (mongoose + express + validator) 如何在将文档保存到 mongoose 之前获取文档的 ID? - How to get the id of a document before even saving it in mongoose? 如何使用mongoose检查记录/文档是否已存在于MongoDB? - How to check if a record/document already exists in MongoDB using mongoose? 使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z Model 检查文档是否存在于集合中 - Check if document exists in collection with Mongoose Model 如何检查猫鼬中是否存在值 - How to check if a value exists in mongoose 如何检查具有某些字段的文档是否已存在于 MongoDB 数据库中? - How to check if document with certain fields already exists in the MongoDB database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM