问题描述
我正在尝试在我的角度组件中上传和显示图像。对于后端,我使用 Mongoose、Express、MongoDB 和 Multer 来上传图像。我已经测试了我的后端,它工作正常。在我的 Angular 组件中,当我尝试上传图像时,它成功上传到我的服务器并保存到数据库中。但是,它没有显示在我的组件中,当我单击“添加图像”按钮时,它会在我的图像服务中引发“无法读取未定义的属性 _id”错误。 这是我的图像服务的代码:
import { Injectable } from '@angular/core';
import { Observable,throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { HttpErrorResponse,HttpClient } from '@angular/common/http';
import { gallery } from '../post/gallery';
import { Subject } from "rxjs";
@Injectable({
providedIn: 'root'
})
export class ImageService {
private images: gallery[] = [];
private images$ = new Subject<gallery[]>();
constructor(private http: HttpClient) { }
addImages(name: string,image: File): void {
const imageData = new FormData();
imageData.append("name",name);
imageData.append("image",image,name);
this.http.post<{ gallery: gallery }>('http://localhost:3000/gallery/archive',imageData)
.subscribe((imageData) => {
const gallery: gallery = {
_id: imageData.gallery._id,//WHERE THE ERROR OCCURS
name: name,avatar: imageData.gallery.avatar,};
this.images.push(gallery);
this.images$.next(this.images);
});
}
其他一切都很顺利。当我单击添加图像时,图像被上传到服务器并保存到数据库中,但发生此错误。有人可以解释是什么导致了这个错误,我该如何解决?非常感谢任何帮助,非常感谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)