any is not assignable to parameter of type (response: Response) => any
On my mean stack project I am trying to display the contents within my database onto a table on my html page.
Here is a sample record from my database i made on mlab:
{
"_id": {
"$oid": "5befa2ad59ef330bc8568373"
},
"player": "loco",
"rank": 1,
"score": 200,
"time": "1d 2hrs",
"gamesPlayed": "League",
"status": "online",
"__v": 0
}
This information should be displayed in my table like this:
Player.component.html:
<tr>
<td>Player</td>
<td>Rank</td>
<td>Score</td>
<td>Time</td>
<td>Games Played</td>
<td>Status</td>
</tr>
<tr>
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr> //For each player create new row on table displaying player info
Im unable to figure out how to display player info for every player i have on my database.
-
-
The work i have done so far is this:
I created a service file player.service.ts
On this file im getting an error that says:
Argument of type '(response: import("myapp/node_modules/@angular/http/src/static_response").Response) => any' is not assignable to parameter of type '(response: Response) => any'.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class PlayerService {
private _getUrl = "/api/players";
constructor(private _http: Http) { } //instance of http to make requests
getPlayers()
{
return this._http.get(this._getUrl) //call get method passing the url and fetch all players
.map((response: Response)=> response.json()); //response is mapped to json
}
}
-
-
On my player.component.ts file everything is running smoothly:
import { Component, OnInit } from '@angular/core';
import { Player } from '../player';
import { PlayerService } from '../player.service';
@Component({
selector: 'app-player-center',
templateUrl: './player-center.component.html',
styleUrls: ['./player-center.component.css'],
providers: [PlayerService]
})
export class PlayerCenterComponent implements OnInit {
players: Array<Player>; //players is array of type player
selectedPlayer: Player;
constructor(private _playerService: PlayerService) { } //dependancy injection to get playerservice
ngOnInit()
{
this._playerService.getPlayers()
.subscribe(resPlayerData => this.players = resPlayerData);
}
onSelectPlayer(player:any)
{
this.selectedPlayer = player;
console.log(this.selectedPlayer);
}
}
Any ideas on what is causing the error on player.service.ts??
And some help on how I can get all my players into the table on my html page would be great! Thanks
add a comment |
On my mean stack project I am trying to display the contents within my database onto a table on my html page.
Here is a sample record from my database i made on mlab:
{
"_id": {
"$oid": "5befa2ad59ef330bc8568373"
},
"player": "loco",
"rank": 1,
"score": 200,
"time": "1d 2hrs",
"gamesPlayed": "League",
"status": "online",
"__v": 0
}
This information should be displayed in my table like this:
Player.component.html:
<tr>
<td>Player</td>
<td>Rank</td>
<td>Score</td>
<td>Time</td>
<td>Games Played</td>
<td>Status</td>
</tr>
<tr>
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr> //For each player create new row on table displaying player info
Im unable to figure out how to display player info for every player i have on my database.
-
-
The work i have done so far is this:
I created a service file player.service.ts
On this file im getting an error that says:
Argument of type '(response: import("myapp/node_modules/@angular/http/src/static_response").Response) => any' is not assignable to parameter of type '(response: Response) => any'.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class PlayerService {
private _getUrl = "/api/players";
constructor(private _http: Http) { } //instance of http to make requests
getPlayers()
{
return this._http.get(this._getUrl) //call get method passing the url and fetch all players
.map((response: Response)=> response.json()); //response is mapped to json
}
}
-
-
On my player.component.ts file everything is running smoothly:
import { Component, OnInit } from '@angular/core';
import { Player } from '../player';
import { PlayerService } from '../player.service';
@Component({
selector: 'app-player-center',
templateUrl: './player-center.component.html',
styleUrls: ['./player-center.component.css'],
providers: [PlayerService]
})
export class PlayerCenterComponent implements OnInit {
players: Array<Player>; //players is array of type player
selectedPlayer: Player;
constructor(private _playerService: PlayerService) { } //dependancy injection to get playerservice
ngOnInit()
{
this._playerService.getPlayers()
.subscribe(resPlayerData => this.players = resPlayerData);
}
onSelectPlayer(player:any)
{
this.selectedPlayer = player;
console.log(this.selectedPlayer);
}
}
Any ideas on what is causing the error on player.service.ts??
And some help on how I can get all my players into the table on my html page would be great! Thanks
add a comment |
On my mean stack project I am trying to display the contents within my database onto a table on my html page.
Here is a sample record from my database i made on mlab:
{
"_id": {
"$oid": "5befa2ad59ef330bc8568373"
},
"player": "loco",
"rank": 1,
"score": 200,
"time": "1d 2hrs",
"gamesPlayed": "League",
"status": "online",
"__v": 0
}
This information should be displayed in my table like this:
Player.component.html:
<tr>
<td>Player</td>
<td>Rank</td>
<td>Score</td>
<td>Time</td>
<td>Games Played</td>
<td>Status</td>
</tr>
<tr>
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr> //For each player create new row on table displaying player info
Im unable to figure out how to display player info for every player i have on my database.
-
-
The work i have done so far is this:
I created a service file player.service.ts
On this file im getting an error that says:
Argument of type '(response: import("myapp/node_modules/@angular/http/src/static_response").Response) => any' is not assignable to parameter of type '(response: Response) => any'.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class PlayerService {
private _getUrl = "/api/players";
constructor(private _http: Http) { } //instance of http to make requests
getPlayers()
{
return this._http.get(this._getUrl) //call get method passing the url and fetch all players
.map((response: Response)=> response.json()); //response is mapped to json
}
}
-
-
On my player.component.ts file everything is running smoothly:
import { Component, OnInit } from '@angular/core';
import { Player } from '../player';
import { PlayerService } from '../player.service';
@Component({
selector: 'app-player-center',
templateUrl: './player-center.component.html',
styleUrls: ['./player-center.component.css'],
providers: [PlayerService]
})
export class PlayerCenterComponent implements OnInit {
players: Array<Player>; //players is array of type player
selectedPlayer: Player;
constructor(private _playerService: PlayerService) { } //dependancy injection to get playerservice
ngOnInit()
{
this._playerService.getPlayers()
.subscribe(resPlayerData => this.players = resPlayerData);
}
onSelectPlayer(player:any)
{
this.selectedPlayer = player;
console.log(this.selectedPlayer);
}
}
Any ideas on what is causing the error on player.service.ts??
And some help on how I can get all my players into the table on my html page would be great! Thanks
On my mean stack project I am trying to display the contents within my database onto a table on my html page.
Here is a sample record from my database i made on mlab:
{
"_id": {
"$oid": "5befa2ad59ef330bc8568373"
},
"player": "loco",
"rank": 1,
"score": 200,
"time": "1d 2hrs",
"gamesPlayed": "League",
"status": "online",
"__v": 0
}
This information should be displayed in my table like this:
Player.component.html:
<tr>
<td>Player</td>
<td>Rank</td>
<td>Score</td>
<td>Time</td>
<td>Games Played</td>
<td>Status</td>
</tr>
<tr>
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr> //For each player create new row on table displaying player info
Im unable to figure out how to display player info for every player i have on my database.
-
-
The work i have done so far is this:
I created a service file player.service.ts
On this file im getting an error that says:
Argument of type '(response: import("myapp/node_modules/@angular/http/src/static_response").Response) => any' is not assignable to parameter of type '(response: Response) => any'.
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable({
providedIn: 'root'
})
export class PlayerService {
private _getUrl = "/api/players";
constructor(private _http: Http) { } //instance of http to make requests
getPlayers()
{
return this._http.get(this._getUrl) //call get method passing the url and fetch all players
.map((response: Response)=> response.json()); //response is mapped to json
}
}
-
-
On my player.component.ts file everything is running smoothly:
import { Component, OnInit } from '@angular/core';
import { Player } from '../player';
import { PlayerService } from '../player.service';
@Component({
selector: 'app-player-center',
templateUrl: './player-center.component.html',
styleUrls: ['./player-center.component.css'],
providers: [PlayerService]
})
export class PlayerCenterComponent implements OnInit {
players: Array<Player>; //players is array of type player
selectedPlayer: Player;
constructor(private _playerService: PlayerService) { } //dependancy injection to get playerservice
ngOnInit()
{
this._playerService.getPlayers()
.subscribe(resPlayerData => this.players = resPlayerData);
}
onSelectPlayer(player:any)
{
this.selectedPlayer = player;
console.log(this.selectedPlayer);
}
}
Any ideas on what is causing the error on player.service.ts??
And some help on how I can get all my players into the table on my html page would be great! Thanks
edited Nov 24 '18 at 23:45
Neil Lunn
99.4k23175186
99.4k23175186
asked Nov 24 '18 at 23:26
TiagoTiago
526
526
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
On first case you are using a deprecated version of http if in case you upgrade your angular you need to use HttpClient from @angular/common/http for more details check this link
Finally there is no need of mentioning the response type you can directly convert it as json() as my thought
getPlayers()
{
return this._http.get(this._getUrl)
.map((response)=> response.json());
}
This will be fine to work your code - hope this helps you - happy coding :)
Update:
For rxjs 6+ you need to use the operators in different manner
import { map, pipe } from 'rxjs/operators' first change in import statement
getPlayers() : Observable<Players>
{
return this._http.get(this._getUrl)
.pipe(map((response)=> response.data));
}
Try something like this for more info check this - hope it helps :)
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
Whatrxjsversion you are using ?
– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
|
show 1 more comment
You need to import HttpClientModule In app.module to resolve the Error 'NullInjectorError: No provider for Http!'
export class AppModule {
imports: [
HttpClientModule
]
}
You could try your service like : (you dont need to parse to json)
getPlayers(): Observable<any> {
return this._http.get(this._getUrl);
}
Then, in your template :
<tr *ngFor="let player of players">
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr>
Hope this helps!
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to removeproviders: [PlayerService]from your component since you have used@Injectable({providedIn: 'root'})you service will be injected to yourAppModuleso you don't need to add providers again in your component - My suggestion :)
– Rahul
Nov 25 '18 at 18:48
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463288%2fany-is-not-assignable-to-parameter-of-type-response-response-any%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
On first case you are using a deprecated version of http if in case you upgrade your angular you need to use HttpClient from @angular/common/http for more details check this link
Finally there is no need of mentioning the response type you can directly convert it as json() as my thought
getPlayers()
{
return this._http.get(this._getUrl)
.map((response)=> response.json());
}
This will be fine to work your code - hope this helps you - happy coding :)
Update:
For rxjs 6+ you need to use the operators in different manner
import { map, pipe } from 'rxjs/operators' first change in import statement
getPlayers() : Observable<Players>
{
return this._http.get(this._getUrl)
.pipe(map((response)=> response.data));
}
Try something like this for more info check this - hope it helps :)
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
Whatrxjsversion you are using ?
– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
|
show 1 more comment
On first case you are using a deprecated version of http if in case you upgrade your angular you need to use HttpClient from @angular/common/http for more details check this link
Finally there is no need of mentioning the response type you can directly convert it as json() as my thought
getPlayers()
{
return this._http.get(this._getUrl)
.map((response)=> response.json());
}
This will be fine to work your code - hope this helps you - happy coding :)
Update:
For rxjs 6+ you need to use the operators in different manner
import { map, pipe } from 'rxjs/operators' first change in import statement
getPlayers() : Observable<Players>
{
return this._http.get(this._getUrl)
.pipe(map((response)=> response.data));
}
Try something like this for more info check this - hope it helps :)
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
Whatrxjsversion you are using ?
– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
|
show 1 more comment
On first case you are using a deprecated version of http if in case you upgrade your angular you need to use HttpClient from @angular/common/http for more details check this link
Finally there is no need of mentioning the response type you can directly convert it as json() as my thought
getPlayers()
{
return this._http.get(this._getUrl)
.map((response)=> response.json());
}
This will be fine to work your code - hope this helps you - happy coding :)
Update:
For rxjs 6+ you need to use the operators in different manner
import { map, pipe } from 'rxjs/operators' first change in import statement
getPlayers() : Observable<Players>
{
return this._http.get(this._getUrl)
.pipe(map((response)=> response.data));
}
Try something like this for more info check this - hope it helps :)
On first case you are using a deprecated version of http if in case you upgrade your angular you need to use HttpClient from @angular/common/http for more details check this link
Finally there is no need of mentioning the response type you can directly convert it as json() as my thought
getPlayers()
{
return this._http.get(this._getUrl)
.map((response)=> response.json());
}
This will be fine to work your code - hope this helps you - happy coding :)
Update:
For rxjs 6+ you need to use the operators in different manner
import { map, pipe } from 'rxjs/operators' first change in import statement
getPlayers() : Observable<Players>
{
return this._http.get(this._getUrl)
.pipe(map((response)=> response.data));
}
Try something like this for more info check this - hope it helps :)
edited Nov 26 '18 at 2:02
answered Nov 25 '18 at 3:35
RahulRahul
1,1092318
1,1092318
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
Whatrxjsversion you are using ?
– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
|
show 1 more comment
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
Whatrxjsversion you are using ?
– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
When i try running the project it fails to compile with the following error: ERROR in src/app/player.service.ts: error TS2339: Property 'map' does not exist on type 'Observable<Response>'
– Tiago
Nov 25 '18 at 18:45
What
rxjs version you are using ?– Rahul
Nov 25 '18 at 18:46
What
rxjs version you are using ?– Rahul
Nov 25 '18 at 18:46
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
I am using version 6.3.3
– Tiago
Nov 25 '18 at 18:57
1
1
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
For rxjs 6+ the operators are changed little bit - i will update my code please try
– Rahul
Nov 25 '18 at 19:09
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
Ok now on my player.component.ts the ngOnInit() function gives me error at .subscriber(resPlayerData => this.players = resPlayerData); Error: type 'Response' is not assignable to type Player
– Tiago
Nov 25 '18 at 19:46
|
show 1 more comment
You need to import HttpClientModule In app.module to resolve the Error 'NullInjectorError: No provider for Http!'
export class AppModule {
imports: [
HttpClientModule
]
}
You could try your service like : (you dont need to parse to json)
getPlayers(): Observable<any> {
return this._http.get(this._getUrl);
}
Then, in your template :
<tr *ngFor="let player of players">
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr>
Hope this helps!
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to removeproviders: [PlayerService]from your component since you have used@Injectable({providedIn: 'root'})you service will be injected to yourAppModuleso you don't need to add providers again in your component - My suggestion :)
– Rahul
Nov 25 '18 at 18:48
add a comment |
You need to import HttpClientModule In app.module to resolve the Error 'NullInjectorError: No provider for Http!'
export class AppModule {
imports: [
HttpClientModule
]
}
You could try your service like : (you dont need to parse to json)
getPlayers(): Observable<any> {
return this._http.get(this._getUrl);
}
Then, in your template :
<tr *ngFor="let player of players">
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr>
Hope this helps!
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to removeproviders: [PlayerService]from your component since you have used@Injectable({providedIn: 'root'})you service will be injected to yourAppModuleso you don't need to add providers again in your component - My suggestion :)
– Rahul
Nov 25 '18 at 18:48
add a comment |
You need to import HttpClientModule In app.module to resolve the Error 'NullInjectorError: No provider for Http!'
export class AppModule {
imports: [
HttpClientModule
]
}
You could try your service like : (you dont need to parse to json)
getPlayers(): Observable<any> {
return this._http.get(this._getUrl);
}
Then, in your template :
<tr *ngFor="let player of players">
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr>
Hope this helps!
You need to import HttpClientModule In app.module to resolve the Error 'NullInjectorError: No provider for Http!'
export class AppModule {
imports: [
HttpClientModule
]
}
You could try your service like : (you dont need to parse to json)
getPlayers(): Observable<any> {
return this._http.get(this._getUrl);
}
Then, in your template :
<tr *ngFor="let player of players">
<td>{{player.name}}</td>
<td>{{player.rank}}</td>
<td>{{player.score}}</td>
<td>{{player.time}}</td>
<td>{{player.gamesPlayed}}</td>
<td>{{player.status}}</td>
</tr>
Hope this helps!
edited Nov 26 '18 at 10:57
answered Nov 24 '18 at 23:31
freepowderfreepowder
2876
2876
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to removeproviders: [PlayerService]from your component since you have used@Injectable({providedIn: 'root'})you service will be injected to yourAppModuleso you don't need to add providers again in your component - My suggestion :)
– Rahul
Nov 25 '18 at 18:48
add a comment |
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to removeproviders: [PlayerService]from your component since you have used@Injectable({providedIn: 'root'})you service will be injected to yourAppModuleso you don't need to add providers again in your component - My suggestion :)
– Rahul
Nov 25 '18 at 18:48
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
@James is that not the correct answer? if so please explain why. Thanks
– freepowder
Nov 25 '18 at 9:26
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
Sorry for the late reply but i had to install rxjs and then import observable as it was giving me an error on observable. But now when i run the project i am getting this error: Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[PlayerService -> Http]: StaticInjectorError(Platform: core)[PlayerService -> Http]: NullInjectorError: No provider for Http!
– Tiago
Nov 25 '18 at 18:42
@james as per the error suggest - you need to remove
providers: [PlayerService] from your component since you have used @Injectable({providedIn: 'root'}) you service will be injected to your AppModule so you don't need to add providers again in your component - My suggestion :)– Rahul
Nov 25 '18 at 18:48
@james as per the error suggest - you need to remove
providers: [PlayerService] from your component since you have used @Injectable({providedIn: 'root'}) you service will be injected to your AppModule so you don't need to add providers again in your component - My suggestion :)– Rahul
Nov 25 '18 at 18:48
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463288%2fany-is-not-assignable-to-parameter-of-type-response-response-any%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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