如何解决无法读取未定义的属性 _id角度图像上传服务类型错误
我正在尝试在我的角度组件中上传和显示图像。对于后端,我使用 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 举报,一经查实,本站将立刻删除。