Hello I am using Angular 6 to send the 'PUT' request to the server side (node.js), I have multiple PUT requests the problem is
if I am getting a put request from the email the phone numbers are updating as null if I get a put request from phone numbers the updated email is changing to null.
I want to try if the value is null it shouldn't get updated in the MongoDB. How to do it please someone help me. Here is my code.
Angular PUT request for updating the email.
public updatedEmail() {
let userData = {
'providerID': '44',
'firstName': this.info.firstName,
'alternate_email': this.email,
};
let xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://localhost:3000/updateprofile');
xhr.setRequestHeader('content-Type', 'application/json');
xhr.send(JSON.stringify(userData));
console.log('data sent');
}
The PUT request for Updating phone Numbers
public updatedPhone() {
let userData = {
'providerID': '44',
'firstName': this.info.firstName,
'workPhone': this.workNumber,
'homePhone': this.homeNumber,
'mobilePhone': this.mobileNumber
};
let xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://localhost:3000/updateprofile');
xhr.setRequestHeader('content-Type', 'application/json');
xhr.send(JSON.stringify(userData));
console.log('data sent');
}
I am using Node.js as the server side here is my user details schema:
var UserDetailSchema = mongoose.Schema({
providerID: {
type: String
},
firstName: {
type: String
},
alternate_email: {
type: String,
set: deleteEmpty
},
workNumber: {
type: String
},
homeNumber: {
type: String
},
mobileNumber: {
type: String
},
}, {timestamps: true});
var UserDetail = module.exports = mongoose.model('UserDetail',
UserDetailSchema);
module.exports = UserDetail;
I the Routes I am making router.put so based on the provider ID it checks the database and updates it if exists or creates it if doesn't.
router.put('/updateprofile', function (req, res, next) {
console.log(req.body)
var query = { providerID: req.body.providerID},
update = { alternate_email: req.body.alternate_email, homeNumber: req.body.homeNumber, workNumber: req.body.workNumber, mobileNumber: req.body.mobileNumber },
options = { upsert: true };
UserDetail.findOneAndUpdate(query, update, options, function (error, result) {
if (error) throw (err);
});
});
Thanks in advance