在Mongo中定位查询以查找嵌套在对象中的电子邮件

时间:2017-06-20 04:06:36

标签: node.js mongodb express mongoose

我正在尝试查询我的Node / Express&中的端点。 Mongo / Mongoose API。

以下是我正在尝试查询的文档/架构的表示:

{
  "_id" : ObjectId("59474bc3478604a75e1980e2"),
  "initial" : "D",
  "last_name" : "Spitz",
  "first_name" : "Chris",
  "owner_id" : "59474b92478604a75e1980e1",
  "contacts" : [
    {
      "last_name" : "Jones",
      "first_name" : "Tom",
      "_id" : ObjectId("5947fd928389ffb393fbc310"),
      "addresses" : [ ],
      "phones" : [ ],
      "emails" : [
        {
          "email_type" : "home",
          "email_address" : "runner@running.com",
          "_id" : ObjectId("5947fd928389ffb393fbc311")
        },
        {
          "email_type" : "home",
          "email_address" : "jake@samsonite.com",
          "_id" : ObjectId("5947ff9ed8fa7cb4c781acc9")
        }
      ]
    },
    {
      "last_name" : "Smith",
      "first_name" : "Linda",
      "_id" : ObjectId("5947ff9ed8fa7cb4c781acc8"),
      "addresses" : [ ],
      "phones" : [ ],
      "emails" : [
        {
          "email_type" : "home",
          "email_address" : "chris@chrisnakea.com",
          "_id" : ObjectId("5947ff9ed8fa7cb4c781acc9")
        }
      ]
    }
  ]
}

我正在尝试检查文档的“contacts.email”数组中是否存在电子邮件。

这是我正在检查的方式(基于乳清,项目嵌套在上面的模式中):

  Profile.find({'contacts.emails.email_address':req.body.invited_email}, function(err, profile){
    if(err)
      console.log('Error in looking for profile.');
    if(profile.contacts.emails.email_address !== null){
      console.log('Found contact: ' + profile.contacts.emails.email_address);
    } else {
      console.log('Contact does not exist');
    }
  })

但由于某种原因,我在测试时遇到了“TypeError:无法读取未定义的属性'电子邮件'错误”错误。

我在这里缺少什么?

0 个答案:

没有答案