Data not refreshed after Deleting from List

1,283 views
Skip to first unread message

Partha Majumdar

unread,
Sep 24, 2018, 4:55:19 AM9/24/18
to Angular and AngularJS discussion
Dear Sir/Madam,

I am new to Angular. I have started programming using Angular and Node since 17-Sep-2018.

I have a page where I display a list of currencies and allow for deleting individual currencies from the list.

On clicking the Delete button, the onDelete() function is firing properly and calling the DeleteCurrency() service. The message is propagated to my node service and the data is deleted from the database. However, when the reply is received, the list should refresh without the deleted currency. This is not happening.

I am attaching my angular and node code for your kind reference.

Please help.

Regards,
Partha

*** ANGULAR SIDE OF TE CODE ***

deleteCurrency(currencyCode: string) {
this.httpClient.delete<{message: string}>('http://localhost:3000/api/currency/' + currencyCode)
.subscribe(() => {
alert('BEFORE DELETE:' + this.currencyList.length);
const updatedCurrencies = this.currencyList.filter(currency => currency.currencyCode !== currencyCode);
this.currencyList = updatedCurrencies;
alert('AFTER DELETE:' + this.currencyList.length);
this.currencyListUpdated.next([...this.currencyList]);
});
}

*** CODE ON NODE JS ***


router.delete("/:currencyCode", (request, response, next) => {
(async () => {
const SQLCurrencyDeleteStatement = "DELETE CURRENCY " +
" WHERE CURRENCY_CODE = " + "'" + request.params.currencyCode + "';";
console.log(SQLCurrencyDeleteStatement);

returnValue = await executeUpdate(paikariConn, SQLCurrencyDeleteStatement);
console.log(returnValue);
response.status(201).json({
message: "Currency deleted successfully"
});
})();
});


Chris wang

unread,
Sep 24, 2018, 10:22:01 PM9/24/18
to ang...@googlegroups.com
when it delete success,  why not just reinvocation the GET method to get the NOW list?

Partha Majumdar <parth...@gmail.com> 于2018年9月24日周一 下午4:55写道:
--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Sep 24, 2018, 10:45:41 PM9/24/18
to Angular and AngularJS discussion

Hi Partha,

Your code is ok. If the currency is not disappearing from the view, the mistake is not in this part. Are you sure you subscribed to the currencyListUpdated observable?

Regards
Sander

Partha Majumdar

unread,
Sep 24, 2018, 10:59:33 PM9/24/18
to Angular and AngularJS discussion
Dear Mr. Sander,

I have subscribed to the listener in ngOnInit().
The code is given below. I have doubt whether this is okay.

This code I had written in ngOnInit(). Later I moved it to a function so that I could call it from ngOnChange() as well. However, I am certain this approach is not correct.

Please advice.

Regards,
Partha


ngOnInit() {
this.refresh();
}

refresh() {
this.isLoading = true;
this.currencyService.getCurrencies();
this.currencySubscription = this.currencyService.getCurrencyUpdatedListener()
.subscribe( (currencies: Currency[]) => {
this.currencyList = currencies;
this.currencyData.data = this.currencyList;
});
this.currencyData.paginator = this.paginator;
this.currencyData.sort = this.sort;
this.isLoading = false;
}

Partha Majumdar

unread,
Sep 24, 2018, 11:01:04 PM9/24/18
to Angular and AngularJS discussion
The alerts I have placed are never fired.

Sander Elias

unread,
Sep 25, 2018, 12:21:24 AM9/25/18
to Angular and AngularJS discussion
Hi Partha,

> The alerts I have placed are never fired.

You mean the alerts in your first msg? If those are not fired, the response from your node server is not reaching back your app. I just noticed you are returning a 201. This might be the problem. Can you try with a code 200?
(BTW 201 means a successful creation, that's a strange response on a delete...)
Also, you should add an error-handling part in your service. checkout how to handle errors in observables. hint the CatchError operator is your friend.

Regards
Sander


Partha Majumdar

unread,
Sep 25, 2018, 5:48:37 AM9/25/18
to Angular and AngularJS discussion
You are right Mr. Sander.

The service is returning empty response.

I am reading to rectify that.

Thanks for your kind support.

Regards,
Partha
Reply all
Reply to author
Forward
0 new messages