无法将数据放入 MEAN 应用程序
Can't PUT data to MEAN application
我无法更新我的 MEAN 应用程序的类别。单击我的 .html 文件中的更新按钮时,我收到以下错误消息:Cannot read 属性 '_id' of undefined for my service.ts
这是我的component.html:
<div class="categories" *ngFor="let category of categories">
<form>
<input type="text" name="title" [(ngModel)]="category.name">
<textarea name="description" [(ngModel)]="category.description"></textarea>
<button (click)="updateCategory(category._id)">Update</button>
</form>
这是我的component.ts:
updateCategory(_id){
this.appService.updateCategory(this.category);
}
这是我的service.ts:
updateCategory(category: Category){
let payload = {
"id": category._id,
"name": category.name,
"description": category.description
}
console.log(category._id);
this.http.put(this.apiUrl+"/category/"+ category._id, payload, {responseType: 'text'}).subscribe(response => {});}
数据库连接和 API 工作正常。只是我的价值观未定义。我的猜测是它与 *ngFor 使数据异步有关,但我不确定。 如何在service.ts中定义_id、名称和描述?
这里使用的分类是在哪里设置的?
updateCategory(_id){
this.appService.updateCategory(this.category); }
您正在将 id 传递给 updateCategory 方法,但随后传递给 this.category
。
在哪里设置this.category
?
传入类别而不是 id 可能更有意义:
<button (click)="updateCategory(category)">Update</button>
然后:
updateCategory(category){
this.appService.updateCategory(category);
}
试一试。
我无法更新我的 MEAN 应用程序的类别。单击我的 .html 文件中的更新按钮时,我收到以下错误消息:Cannot read 属性 '_id' of undefined for my service.ts
这是我的component.html:
<div class="categories" *ngFor="let category of categories">
<form>
<input type="text" name="title" [(ngModel)]="category.name">
<textarea name="description" [(ngModel)]="category.description"></textarea>
<button (click)="updateCategory(category._id)">Update</button>
</form>
这是我的component.ts:
updateCategory(_id){
this.appService.updateCategory(this.category);
}
这是我的service.ts:
updateCategory(category: Category){
let payload = {
"id": category._id,
"name": category.name,
"description": category.description
}
console.log(category._id);
this.http.put(this.apiUrl+"/category/"+ category._id, payload, {responseType: 'text'}).subscribe(response => {});}
数据库连接和 API 工作正常。只是我的价值观未定义。我的猜测是它与 *ngFor 使数据异步有关,但我不确定。 如何在service.ts中定义_id、名称和描述?
这里使用的分类是在哪里设置的?
updateCategory(_id){
this.appService.updateCategory(this.category); }
您正在将 id 传递给 updateCategory 方法,但随后传递给 this.category
。
在哪里设置this.category
?
传入类别而不是 id 可能更有意义:
<button (click)="updateCategory(category)">Update</button>
然后:
updateCategory(category){
this.appService.updateCategory(category);
}
试一试。