无法更新和删除记录

时间:2018-11-15 20:12:22

标签: angular6 angular2-services angular4-httpclient

我无法使用以下代码更新或删除记录:

服务

UpdateWeatherData(wt:天气){         返回this.http.put(/WeatherModels/Update,wt);     }

DeleteWeatherData(wt : Weather){
        return this.http.delete(`/WeatherModelsDelete/`+wt);
    }

  if(request.url.endsWith('/WeatherModels/Update') && request.method === 'PUT'){
                    // check for fake auth token in header and return user if valid, this security is implemented server side in a real application
                    if(request.headers.get('Authorization')=== 'Bearer xyz@123'){
                           // find items by id in weather data array
                        //let urlParts = request.url.split('/');
                        //let id = parseInt(urlParts[urlParts.length-1]);
                        let WeatherModel = request.body;
                        let id = WeatherModel.ID;
                        for(let i=0;i < WeatherModels.length;i++){
                            // let WeatherModel = WeatherModels[i];
                            if(id === WeatherModels[i].ID){

                                WeatherModels[i].   (WeatherModel);
                                localStorage.setItem('WeatherModels',JSON.stringify(WeatherModels));
                                break;
                            }
                        }

                        return of(new HttpResponse({ status : 200}));
                    } else{

                        return throwError({ error: { message : 'Unauthorised'} } );
                    }

                 }
    here not able to reach the below url

                 if (request.url.match(/\/WeatherModelsDelete\/\d+$/) && request.method === 'DELETE') {

                    if(request.headers.get('Authorization')=== 'Bearer xyz@123'){

                        let WeatherModel = request.body;
                        let id = WeatherModel.ID;
                        for(let i=0;i < WeatherModels.length;i++){
                            let WeatherModel = WeatherModels[i];
                            if(WeatherModel[i].ID===id){
                                //update the collection
                                WeatherModels.splice(i,1);
                                localStorage.setItem('WeatherModels',JSON.stringify(WeatherModels));
                                break;
                            }
                        }

                        return of(new HttpResponse({ status : 200}));
                    } else{

                        return throwError({ error: { message : 'Unauthorised'} } );
                    }

                 }

0 个答案:

没有答案