##Angularにて、サーバーからデータを取得してくる処理を書いているのですが、下記エラーが発生しました。
ErrorCode
1ERROR in src/app/app.component.ts:47:51 - error TS2769: No overload matches this call. 2 Overload 1 of 5, '(observer?: PartialObserver<Comment[]>): Subscription', gave the following error. 3 Argument of type '(comments: Comment[]) => void' is not assignable to parameter of type 'PartialObserver<Comment[]>'. 4 Property 'complete' is missing in type '(comments: Comment[]) => void' but required in type 'CompletionObserver<Comment[]>'. 5 Overload 2 of 5, '(next?: (value: Comment[]) => void, error?: (error: any) => void, complete?: () => void): Subscription', gave the following error. 6 Argument of type '(comments: Comment[]) => void' is not assignable to parameter of type '(value: Comment[]) => void'. 7 Types of parameters 'comments' and 'value' are incompatible. 8 Type 'Comment[]' is not assignable to type 'import("F:/Github/Angular-async-mysql/src/app/class/comment").Comment[]'. 9 Type 'Comment' is missing the following properties from type 'Comment': id, date, message, uid, user 10 11 47 this.uniqueService.readComments().subscribe((comments:Comment[])=>{ 12 ~~~~~~~~~~~~~~~~~~~~~~~ 13 14 node_modules/rxjs/internal/types.d.ts:64:5 15 64 complete: () => void; 16 ~~~~~~~~ 17 'complete' is declared here.
実行したコードは以下です。
appComponent
1export class AppComponent implements OnInit { 2 comments: Comment[]; 3 constructor(private uniqueService: UniqueService) { } 4 ngOnInit() { 5 this.uniqueService.readComments().subscribe((comments:Comment[])=>{ 6 this.comments = comments; 7 console.log(this.comments); 8 }); 9 } 10 createComment(form){ 11 this.uniqueService.createComment(form.value); 12 } 13}
UniqueService
1export class UniqueService { 2 constructor(public http: HttpClient) { } 3 ip=IpService.getIPAddress() 4 readComments(): Observable<Comment[]>{ 5 return this.http.get<Comment[]>(this.ip + 'chat-CRUD/read.php'); 6 } 7 createComment(comment: Comment): Observable<Comment>{ 8 return this.http.post<Comment>(this.ip + 'chat-CRUD/create.php', comment); 9 } 10}
CommentClass
1import { User } from './user'; 2export class Comment { 3 id: number; 4 date: number; 5 message: string; 6 uid: number; 7 user: User; 8}
UserClass
1export class User { 2 initial: string; 3 constructor(public uid: number, public name: string) { 4 this.initial = name.slice(0, 1); 5 } 6}
readPHP
1<?php 2require 'database.php'; 3 4$comments = []; 5$sql = "SELECT comments.id as id, comments.date as date, comments.message as message, comments.userId as userId, users.name as name FROM comments JOIN users ON comments.userId = users.id"; 6 7if($result = mysqli_query($con,$sql)) { 8 $i = 0; 9 while($row = mysqli_fetch_assoc($result)) { 10 $comments[$i]['id'] = $row['id']; 11 $comments[$i]['date'] = $row['date']; 12 $comments[$i]['message'] = $row['message']; 13 $comments[$i]['uid'] = $row['userId']; 14 $comments[$i]['user']['uid'] = $row['userId']; 15 $comments[$i]['user']['name'] = $row['name']; 16 $i++; 17 } 18 19 echo json_encode($comments); 20} else { 21 http_response_code(404); 22}
以下がread.phpにアクセスした際に受け取れるデータです。
[{"id":"1","date":"2021-03-01 08:32:22","message":"\u304a\u3064\u3067\u30fc\u3059\uff01","uid":"1","user":{"uid":"1","name":"\u4f50\u85e4 \u8003\u592a"}},{"id":"2","date":"2021-03-01 08:32:22","message":"\u4f5c\u696d\u7d42\u308f\u3063\u305f\u30fc\uff1f","uid":"1","user":{"uid":"1","name":"\u4f50\u85e4 \u8003\u592a"}},{"id":"3","date":"2021-03-01 08:32:48","message":"\u304a\u3064\u3067\u30fc\u3059\uff01","uid":"2","user":{"uid":"2","name":"\u68ee\u4e95 \u5c07\u88d5"}},{"id":"4","date":"2021-03-01 08:32:48","message":"\u7d42\u308f\u3063\u3066\u307e\u30fc\u3059","uid":"2","user":{"uid":"2","name":"\u68ee\u4e95 \u5c07\u88d5"}},{"id":"5","date":"2021-03-02 07:30:39","message":"aaaaaaaaa","uid":"1","user":{"uid":"1","name":"\u4f50\u85e4 \u8003\u592a"}}]
#####また、以下のコードでも実行してみたのですが、エラーが出ました。
appComponent
1 ngOnInit() { 2 this.uniqueService.readComments().subscribe(comments => : this.comments = comments); 3 }
ErrorCode
1ERROR in src/app/app.component.ts:51:61 - error TS2322: Type 'Comment[]' is not assignable to type 'import("F:/Github/Angular-async-mysql/src/app/class/comment").Comment[]'. 2 3 51 this.uniqueService.readComments().subscribe(comments => this.comments = comments); 4 ~~~~~~~~~~~~~ 5 src/app/app.component.ts:51:61 - error TS2322: Type 'Comment[]' is not assignable to type 'import("F:/Github/Angular-async-mysql/src/app/class/comment").Comment[]'. 6 Type 'Comment' is missing the following properties from type 'Comment': id, date, message, uid, user 7 8 51 this.uniqueService.readComments().subscribe(comments => this.comments = comments); 9 ~~~~~~~~~~~~~
どこの記述が間違っているのでしょうか?
色々調べてもよくわからず困っています。ぜひとも助けてください。お願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。