Reputation: 1841
I have an endpoint where I'm trying to update a phone number in all "Contacts" documents after updating a user "Profile" document that shares their phone numbers.
Based on the documents, I believe I have the right structure. The "Profiles" document updates fine, but "Contacts" does not.
Here is the code:
router.route('/updatephone/:id')
// UPDATE PHONE
.put(function(req, res){
Profile.findOne({'owner_id':req.params.id}, function(err, profile){
if(err)
res.send(err);
var phone = profile.phones.id(req.body._id)
phone.phone_number = req.body.phone_number;
phone.phone_type = req.body.phone_type;
profile.save(function(err){
if(err)
res.send(err);
res.json(profile);
}).then(function(){
console.log('Phone ID: ' + req.body._id);
console.log('Phone Number: ' + req.body.phone_number);
console.log('Phone_Type: ' + req.body.phone_type);
Contacts.update(
{'shared.phones._id':req.body._id},
{$set:{
'shared.phones.$.phone_number':req.body.phone_number,
'shared.phones.$.phone_type': req.body.phone_type
}},
{'multi':true})
})
});
});
Here is an example of the document structure for "Contacts".
{
"_id" : ObjectId("59c3dac6764e8d7135e744e3"),
"shared" : {
"_id" : ObjectId("59c3dac6764e8d7135e744e5"),
"businesses" : [ ],
"addresses" : [ ],
"phones" : [
{
"phone_number" : "5555555555",
"phone_type" : "mobile",
"_id" : ObjectId("59c200829aa4486971926ce9")
},
{
"phone_number" : "4444444444",
"phone_type" : "business",
"_id" : ObjectId("59c200ad9aa4486971926cea")
}
],
"emails" : [ ],
"first_name" : "Joe",
"invite_id" : "PaulSmith59c1ff9f9aa4486971926ce7",
"last_name" : "Public",
"link" : "PaulSmith59c1ff9f9aa4486971926ce7"
},
"last_name" : "Smith",
"first_name" : "Paul",
"owner_id" : "59c1ff9f9aa4486971926ce7",
"emails" : [
{
"email_address" : "[email protected]",
"email_type" : "home",
"_id" : ObjectId("59c3dac6764e8d7135e744e4")
}
],
"__v" : 1,
"addresses" : [ ],
"businesses" : [ ],
"phones" : [ ]
}
Upvotes: 0
Views: 854
Reputation: 12071
You have passed a callback function to profile.save(). Therefore, it won't return a promise so your then() method won't fire.
Try performing the Contact.update() in the profile.save() callback:
profile.save(function(err) {
if (err) res.send(err)
console.log('Phone ID: ' + req.body._id)
console.log('Phone Number: ' + req.body.phone_number)
console.log('Phone_Type: ' + req.body.phone_type)
Contacts.update(
{
'shared.phones._id': req.body._id
},
{
$set: {
'shared.phones.$.phone_number': req.body.phone_number,
'shared.phones.$.phone_type': req.body.phone_type
}
},
{
'multi': true
},
function(err) {
res.json(profile)
}
)
})
You could do it with promises too:
profile.save()
.then(function() {
console.log('Phone ID: ' + req.body._id)
console.log('Phone Number: ' + req.body.phone_number)
console.log('Phone_Type: ' + req.body.phone_type)
return Contacts.update(
{
'shared.phones._id': req.body._id
},
{
$set: {
'shared.phones.$.phone_number': req.body.phone_number,
'shared.phones.$.phone_type': req.body.phone_type
}
},
{
'multi': true
}
)
})
.then(function() {
res.json(profile)
})
.catch(function(err) {
res.send(err)
})
Upvotes: 1