mongoose保存不在if语句的第二部分工作

时间:2017-09-25 15:51:24

标签: node.js mongodb express mongoose

我的Node / Express API中有一个函数,我首先检查我的“Contacts”集合中是否存在“Contact”。如果是,我会做更新。如果查询中的电子邮件不存在,那么我创建一个新的。

如果存在联系人,则contact.save()有效。但是如果联系人不存在,contact.save()会生成一条错误消息,说“contact.save()不是函数。”

我看不出我做错了什么。希望有人在那里可以很容易地看到它,并给我一些建议。

以下是代码:

  // SEARCH IF CONTACT EXISTS IN SENDERS CONTACTS -  BY OWNERID AND EMAIL IN EMAILS
  Contact.findOne({'owner_id': req.body.owner_id, 'emails.email_address':req.body.invited_email}, function(err, contact){
    if(err)
      console.log('Error in looking for contact.');


    // IF A CONTACT EXISTS ---
    if(contact){

      console.log('--> CONTACT FOUND');

          // SET CONTACT SHARED INFORMATION
          contact.shared.invite_id = req.body.invited_first_name + req.body.invited_last_name + req.body.owner_id;
          contact.shared.profile_id = req.body.profile_id;
          contact.shared.first_name = req.body.first_name;
          contact.shared.company = req.body.company;
          contact.shared.title = req.body.title;
          contact.shared.last_name = req.body.last_name;
          contact.shared.initial = req.body.initial;
          contact.shared.linkedin = req.body.linkedin;
          contact.shared.google = req.body.google;
          contact.shared.facebook = req.body.facebook;
          contact.shared.pinterest = req.body.pinterest;
          contact.shared.twitter = req.body.twitter;
          contact.shared.emails = req.body.emails;
          contact.shared.phones = req.body.phones;
          contact.shared.addresses = req.body.addresses;
          contact.shared.link = req.body.invited_first_name + req.body.invited_last_name + req.body.owner_id;


          // ADD TO ALERT INFORMING SENDER THAT HE/SHE SHARED INFO
          alerts.genShare(req.body.owner_id, req.body.invited_first_name, req.body.invited_last_name, req.body.invited_email);

          // SAVE CONTACT INFORMATION
          contact.save(function(err){
            if(err)
              res.send(err);
            res.json(contact);
          });


          // IF INVITED IS NOT A USER - CREATE USER ACCOUNT
          if(!inviteIsUser){

            var phones = req.body.phones;
            var emails = req.body.emails;
            var addresses = req.body.addresses;

            var email = [{email_address:req.body.invited_email, email_type:'home'}];

            var share = {
              invite_id:req.body.invited_first_name + req.body.invited_last_name + req.body.owner_id,
              profile_id:req.body.profile_id,
              first_name:req.body.first_name, 
              last_name:req.body.last_name,
              initial:req.body.initial,
              birthday:req.body.birthday,
              highschool:req.body.highschool,
              college:req.body.college,
              facebook:req.body.facebook,
              linkedin:req.body.linkedin,
              google:req.body.google,
              pinterest:req.body.pinterest,
              user_image:req.body.user_image,
              emails:emails, 
              addresses:addresses, 
              phones:phones,
              shared:{}
            };

            var newContact = {
                emails: email,
                profile_id: req.body.profile_id,
                first_name: req.body.invited_first_name,
                last_name : req.body.invited_last_name,
                shared:share
            };

            //CREATE NEW USER AND ADD CONTACT FOR PERSON BEING SHARED WITH
            newUser = userCreate.genUser(req.body.invited_email, newContact);

          }


    } else {

      var contact = new Contact();

      //IF CONTACT EMAIL IS NOT FOUND, A NEW CONTACT IS CREATED
      console.log('Contact NOT found');

      var phones = req.body.phones;
      var emails = req.body.emails;
      var addresses = req.body.addresses;

      var email = [{email_address:req.body.invited_email, email_type:'home'}];

      var share = {
        invite_id:req.body.invited_first_name + req.body.invited_last_name + req.body.owner_id,
        profile_id:req.body.profile_id,
        first_name:req.body.first_name, 
        last_name:req.body.last_name,
        initial:req.body.initial,
        birthday:req.body.birthday,
        highschool:req.body.highschool,
        college:req.body.college,
        facebook:req.body.facebook,
        linkedin:req.body.linkedin,
        google:req.body.google,
        pinterest:req.body.pinterest,
        user_image:req.body.user_image,
        emails:emails, 
        addresses:addresses, 
        phones:phones,
        link:req.body.invited_first_name + req.body.invited_last_name + req.body.owner_id,
        shared:{}
      };


      contact = {
          emails: email,
          owner_id:req.body.owner_id,
          profile_id:req.body.profile_id,
          first_name: req.body.invited_first_name,
          last_name : req.body.invited_last_name,
          shared:share
      };

      console.log('Contact: ', contact);

      // SAVE CONTACT INFORMATION
      contact.save(function(err){
        if(err)
          res.send(err);
        res.json(contact);
      });



      // // IF INVITED IS NOT A USER - CREATE USER ACCOUNT
      // if(!inviteIsUser){
      //   newUser = userCreate.genUser(req.body.invited_email, contact);
      // }


      // ADD TO ALERT INFORMING SENDER THAT HE/SHE SHARED INFO
      alerts.genShare(req.body.owner_id, req.body.invited_first_name, req.body.invited_last_name, req.body.invited_email);

   }

})

1 个答案:

答案 0 :(得分:2)

您已使用对象重写了contact

contact = {
          emails: email,
          owner_id:req.body.owner_id,
          profile_id:req.body.profile_id,
          first_name: req.body.invited_first_name,
          last_name : req.body.invited_last_name,
          shared:share
      };

然后你尝试调用那里没有的函数save。