質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Angular

Angularは、JavaScriptフレームワークです。AngularJSの後継であり、TypeScriptベースで実装されています。機能ごとに実装を分けやすく、コードの見通しが良いコンポーネント指向です。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

1回答

2195閲覧

Angularでサーバーからデータを取得してくる処理。No overload matches this call.

NodeTECH.

総合スコア0

Angular

Angularは、JavaScriptフレームワークです。AngularJSの後継であり、TypeScriptベースで実装されています。機能ごとに実装を分けやすく、コードの見通しが良いコンポーネント指向です。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/03/02 21:39

編集2021/03/02 21:46

##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 ~~~~~~~~~~~~~

どこの記述が間違っているのでしょうか?
色々調べてもよくわからず困っています。ぜひとも助けてください。お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

Commentがクラスではなくinterfaceまたはtypeの間違いではないですか?

投稿2021/03/10 15:55

mosapride

総合スコア1480

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問