ERROR TypeError: Cannot read property 'getUsers' of undefined










0















listuser.component.ts



import Component, OnInit from '@angular/core';
import User from 'src/app/user';
import UserService from 'src/app/shared_service/user.service';

@Component(
selector: 'app-listuser',
templateUrl: './listuser.component.html',
styleUrls: ['./listuser.component.css']
)
export class ListuserComponent implements OnInit
private users:User;
_userService: any;
constructor(private_userService:UserService)

ngOnInit()
this._userService.getUsers().subscribe((users)=>
console.log(users);
this.users=users;
,(error)=>
console.log(error);
)





user.service.ts



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from '../user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
_http: any;
constructor(private_http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');





after Run this ..the error:
ERROR TypeError: Cannot read property 'getUsers' of undefined
at ListuserComponent.push../src/app/components/listuser/listuser.component.ts.ListuserComponent.ngOnInit (listuser.component.ts:16)
at checkAndUpdateDirectiveInline (core.js:18620)
at checkAndUpdateNodeInline (core.js:19884)
at checkAndUpdateNode (core.js:19846)
at debugCheckAndUpdateNode (core.js:20480)
at debugCheckDirectivesFn (core.js:20440)
at Object.eval [as updateDirectives] (ListuserComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
at checkAndUpdateView (core.js:19828)
at callViewAction (core.js:20069)










share|improve this question






















  • you has a type error is private<space>_http and private<space>_userService

    – Eliseo
    Nov 11 '18 at 10:20











  • NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:19















0















listuser.component.ts



import Component, OnInit from '@angular/core';
import User from 'src/app/user';
import UserService from 'src/app/shared_service/user.service';

@Component(
selector: 'app-listuser',
templateUrl: './listuser.component.html',
styleUrls: ['./listuser.component.css']
)
export class ListuserComponent implements OnInit
private users:User;
_userService: any;
constructor(private_userService:UserService)

ngOnInit()
this._userService.getUsers().subscribe((users)=>
console.log(users);
this.users=users;
,(error)=>
console.log(error);
)





user.service.ts



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from '../user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
_http: any;
constructor(private_http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');





after Run this ..the error:
ERROR TypeError: Cannot read property 'getUsers' of undefined
at ListuserComponent.push../src/app/components/listuser/listuser.component.ts.ListuserComponent.ngOnInit (listuser.component.ts:16)
at checkAndUpdateDirectiveInline (core.js:18620)
at checkAndUpdateNodeInline (core.js:19884)
at checkAndUpdateNode (core.js:19846)
at debugCheckAndUpdateNode (core.js:20480)
at debugCheckDirectivesFn (core.js:20440)
at Object.eval [as updateDirectives] (ListuserComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
at checkAndUpdateView (core.js:19828)
at callViewAction (core.js:20069)










share|improve this question






















  • you has a type error is private<space>_http and private<space>_userService

    – Eliseo
    Nov 11 '18 at 10:20











  • NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:19













0












0








0








listuser.component.ts



import Component, OnInit from '@angular/core';
import User from 'src/app/user';
import UserService from 'src/app/shared_service/user.service';

@Component(
selector: 'app-listuser',
templateUrl: './listuser.component.html',
styleUrls: ['./listuser.component.css']
)
export class ListuserComponent implements OnInit
private users:User;
_userService: any;
constructor(private_userService:UserService)

ngOnInit()
this._userService.getUsers().subscribe((users)=>
console.log(users);
this.users=users;
,(error)=>
console.log(error);
)





user.service.ts



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from '../user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
_http: any;
constructor(private_http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');





after Run this ..the error:
ERROR TypeError: Cannot read property 'getUsers' of undefined
at ListuserComponent.push../src/app/components/listuser/listuser.component.ts.ListuserComponent.ngOnInit (listuser.component.ts:16)
at checkAndUpdateDirectiveInline (core.js:18620)
at checkAndUpdateNodeInline (core.js:19884)
at checkAndUpdateNode (core.js:19846)
at debugCheckAndUpdateNode (core.js:20480)
at debugCheckDirectivesFn (core.js:20440)
at Object.eval [as updateDirectives] (ListuserComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
at checkAndUpdateView (core.js:19828)
at callViewAction (core.js:20069)










share|improve this question














listuser.component.ts



import Component, OnInit from '@angular/core';
import User from 'src/app/user';
import UserService from 'src/app/shared_service/user.service';

@Component(
selector: 'app-listuser',
templateUrl: './listuser.component.html',
styleUrls: ['./listuser.component.css']
)
export class ListuserComponent implements OnInit
private users:User;
_userService: any;
constructor(private_userService:UserService)

ngOnInit()
this._userService.getUsers().subscribe((users)=>
console.log(users);
this.users=users;
,(error)=>
console.log(error);
)





user.service.ts



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from '../user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
_http: any;
constructor(private_http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');





after Run this ..the error:
ERROR TypeError: Cannot read property 'getUsers' of undefined
at ListuserComponent.push../src/app/components/listuser/listuser.component.ts.ListuserComponent.ngOnInit (listuser.component.ts:16)
at checkAndUpdateDirectiveInline (core.js:18620)
at checkAndUpdateNodeInline (core.js:19884)
at checkAndUpdateNode (core.js:19846)
at debugCheckAndUpdateNode (core.js:20480)
at debugCheckDirectivesFn (core.js:20440)
at Object.eval [as updateDirectives] (ListuserComponent_Host.ngfactory.js? [sm]:1)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:20432)
at checkAndUpdateView (core.js:19828)
at callViewAction (core.js:20069)







angular hibernate spring-boot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '18 at 10:16









Rohit PrajapatiRohit Prajapati

84




84












  • you has a type error is private<space>_http and private<space>_userService

    – Eliseo
    Nov 11 '18 at 10:20











  • NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:19

















  • you has a type error is private<space>_http and private<space>_userService

    – Eliseo
    Nov 11 '18 at 10:20











  • NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:19
















you has a type error is private<space>_http and private<space>_userService

– Eliseo
Nov 11 '18 at 10:20





you has a type error is private<space>_http and private<space>_userService

– Eliseo
Nov 11 '18 at 10:20













NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

– Eliseo
Nov 11 '18 at 11:19





NOTE: You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

– Eliseo
Nov 11 '18 at 11:19












3 Answers
3






active

oldest

votes


















0














Looks like you have forgotten to add blank space after private and before _userService in your constructor:



constructor(private _userService:UserService) 





share|improve this answer























  • Please mark an answer as complete and up vote the answers which you think are right.

    – Stundji
    Nov 11 '18 at 10:58


















0














you need to add space between private and _userService, so this



constructor(private_userService:UserService) 


should be



constructor(private _userService: UserService) 





share|improve this answer























  • Thank you very much sir

    – Rohit Prajapati
    Nov 11 '18 at 10:49


















0














There are couple issues in your service class -



  1. Space between private and _http

  2. Remove additional declared instance variable _http: any;

Modified code



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from './user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
constructor(private _http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');








share|improve this answer


















  • 1





    @Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:11











  • @Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

    – Sunil Singh
    Nov 11 '18 at 11:14











  • Sorry, I'll put the comment to Rohit

    – Eliseo
    Nov 11 '18 at 11:18










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247732%2ferror-typeerror-cannot-read-property-getusers-of-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Looks like you have forgotten to add blank space after private and before _userService in your constructor:



constructor(private _userService:UserService) 





share|improve this answer























  • Please mark an answer as complete and up vote the answers which you think are right.

    – Stundji
    Nov 11 '18 at 10:58















0














Looks like you have forgotten to add blank space after private and before _userService in your constructor:



constructor(private _userService:UserService) 





share|improve this answer























  • Please mark an answer as complete and up vote the answers which you think are right.

    – Stundji
    Nov 11 '18 at 10:58













0












0








0







Looks like you have forgotten to add blank space after private and before _userService in your constructor:



constructor(private _userService:UserService) 





share|improve this answer













Looks like you have forgotten to add blank space after private and before _userService in your constructor:



constructor(private _userService:UserService) 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 10:26









StundjiStundji

442212




442212












  • Please mark an answer as complete and up vote the answers which you think are right.

    – Stundji
    Nov 11 '18 at 10:58

















  • Please mark an answer as complete and up vote the answers which you think are right.

    – Stundji
    Nov 11 '18 at 10:58
















Please mark an answer as complete and up vote the answers which you think are right.

– Stundji
Nov 11 '18 at 10:58





Please mark an answer as complete and up vote the answers which you think are right.

– Stundji
Nov 11 '18 at 10:58













0














you need to add space between private and _userService, so this



constructor(private_userService:UserService) 


should be



constructor(private _userService: UserService) 





share|improve this answer























  • Thank you very much sir

    – Rohit Prajapati
    Nov 11 '18 at 10:49















0














you need to add space between private and _userService, so this



constructor(private_userService:UserService) 


should be



constructor(private _userService: UserService) 





share|improve this answer























  • Thank you very much sir

    – Rohit Prajapati
    Nov 11 '18 at 10:49













0












0








0







you need to add space between private and _userService, so this



constructor(private_userService:UserService) 


should be



constructor(private _userService: UserService) 





share|improve this answer













you need to add space between private and _userService, so this



constructor(private_userService:UserService) 


should be



constructor(private _userService: UserService) 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 10:26









Artyom AmiryanArtyom Amiryan

1,9271214




1,9271214












  • Thank you very much sir

    – Rohit Prajapati
    Nov 11 '18 at 10:49

















  • Thank you very much sir

    – Rohit Prajapati
    Nov 11 '18 at 10:49
















Thank you very much sir

– Rohit Prajapati
Nov 11 '18 at 10:49





Thank you very much sir

– Rohit Prajapati
Nov 11 '18 at 10:49











0














There are couple issues in your service class -



  1. Space between private and _http

  2. Remove additional declared instance variable _http: any;

Modified code



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from './user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
constructor(private _http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');








share|improve this answer


















  • 1





    @Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:11











  • @Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

    – Sunil Singh
    Nov 11 '18 at 11:14











  • Sorry, I'll put the comment to Rohit

    – Eliseo
    Nov 11 '18 at 11:18















0














There are couple issues in your service class -



  1. Space between private and _http

  2. Remove additional declared instance variable _http: any;

Modified code



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from './user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
constructor(private _http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');








share|improve this answer


















  • 1





    @Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:11











  • @Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

    – Sunil Singh
    Nov 11 '18 at 11:14











  • Sorry, I'll put the comment to Rohit

    – Eliseo
    Nov 11 '18 at 11:18













0












0








0







There are couple issues in your service class -



  1. Space between private and _http

  2. Remove additional declared instance variable _http: any;

Modified code



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from './user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
constructor(private _http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');








share|improve this answer













There are couple issues in your service class -



  1. Space between private and _http

  2. Remove additional declared instance variable _http: any;

Modified code



import Injectable from '@angular/core';
import Http, Response, Headers, RequestOptions from '@angular/http';
import Observable from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

import User from './user';

@Injectable(
providedIn: 'root'
)
export class UserService
private baseUrl:string="http://localhost:8080/api";
private headers = new Headers('Content-Type':'application/json');
private options = new RequestOptions(headers:this.headers);
constructor(private _http:Http)

getUsers()

return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


getUser(id:Number)

return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


deleteUser(id:Number)

return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


createUsers(user:User)

return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


updateUsers(user:User)

return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
.catch(this.errorHandler);


errorHandler(error:Response)'SERVER ERROR');









share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 10:29









Sunil SinghSunil Singh

6,1922626




6,1922626







  • 1





    @Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:11











  • @Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

    – Sunil Singh
    Nov 11 '18 at 11:14











  • Sorry, I'll put the comment to Rohit

    – Eliseo
    Nov 11 '18 at 11:18












  • 1





    @Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

    – Eliseo
    Nov 11 '18 at 11:11











  • @Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

    – Sunil Singh
    Nov 11 '18 at 11:14











  • Sorry, I'll put the comment to Rohit

    – Eliseo
    Nov 11 '18 at 11:18







1




1





@Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

– Eliseo
Nov 11 '18 at 11:11





@Sunil, You're using Angular 2 syntax. Sometime ago Angular 5, 6 and 7 make appearance. The things changes for best. Now it's not used Http, else HttpClient, not used Rxjs 5, else Rxjs 6...

– Eliseo
Nov 11 '18 at 11:11













@Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

– Sunil Singh
Nov 11 '18 at 11:14





@Eliseo - This syntax is not mine instead shared by Rohit, we are giving the solution for the issue he had. Rohit might be using old version.

– Sunil Singh
Nov 11 '18 at 11:14













Sorry, I'll put the comment to Rohit

– Eliseo
Nov 11 '18 at 11:18





Sorry, I'll put the comment to Rohit

– Eliseo
Nov 11 '18 at 11:18

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247732%2ferror-typeerror-cannot-read-property-getusers-of-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?