我有一个formular的复选框,我想将数据存储在数据库中,其中包含更多ID的atribut。因此,在我看来,最好的方法是使用类型数组,以便以后使用输入的ID。但是在Angular中我在matchs.service.ts中有addMatch函数,我有错误:
Argument of type 'Player[]' is not assignable to parameter of type 'string | Blob'.
Type 'Player[]' is not assignable to type 'string'.
这是函数,我有问题,其中Player是模型:
addMatch(dateOfMatch: Date,
playersPlayed: Array<Player>) {
const matchData = new FormData();
matchData.append("dateOfMatch", dateOfMatch.toString());
matchData.append("playersPlayed", playersPlayed);
this.http
.post<{ message: string; match: Match }>(
"http://localhost:3000/api/matchs",
matchData
)
.subscribe(responseData => {
this.router.navigate(["/"]);
});
}
在我的HTML中,我从用户那里获取数据在这里:
<mat-form-field>
<mat-select formControlName="playersPlayed" placeholder="Players" multiple>
<mat-option *ngFor="let player of players" [value]="player.id">{{player.name}} {{player.surename}} ({{player.teamName}})</mat-option>
</mat-select>
<mat-error *ngIf="form.get('playersPlayed').invalid">Please enter a players who played.</mat-error>
</mat-form-field>
我做错了什么?谢谢。
从FormData文档中有关append函数的value参数:
这可以是USVString或Blob(包括诸如File之类的子类)。
资料来源:FormData.append()
所以我假设你的情况下字符串选项是正确的。我会尝试对Player数组进行字符串化,我想这可行:
matchData.append("playersPlayed", JSON.stringify(playersPlayed));