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

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

如果联系人存在,contact.save()将起作用。 但是,如果联系人不存在,contact.save()会生成一个错误消息,说“contact.save()不是一个函数。

我不明白我做错了什么。 希望在那里的人能够看到它很容易,给我一些build议。

这里是代码:

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

       

网上收集的解决方案 "mongoose保存不工作在if语句的第二部分"