I am facing a straightforward issue that I need help with in terms of using observables effectively. My goal is to search my database for a specific property value, and if it exists, update it with new data. If it does not exist, then I want to add the new data with the property value. I am currently working with Angular 10.0.7 and leveraging httpClient.
Here's what I intend to achieve:
saveData(data: DataObject) {
this.httpClient.get<DataObject>(
this.DATA_BASE_URL + this.user.username + ".json"
).subscribe(response => {
response.propertyName = data;
if (response.hasOwnProperty("propertyName")) {
this.httpClient.patch(
this.TEST_DB_URL + this.user.username + ".json",
response
).subscribe(r => {
console.log(r);
})
} else {
this.httpClient.put(
this.TEST_DB_URL + this.user.username + ".json",
response
).subscribe(r => {
console.log(r);
})
}
})
}
I am unsure if nesting http calls within the subscribe() method is the best approach. Could you suggest a more conventional or efficient way to implement this logic? Thank you for your assistance!