显示图像时,Object.parse位置0处JSON中的意外令牌

时间:2018-07-07 07:10:49

标签: node.js angular mongodb express mean

我正在尝试将图像存储在mongodb中并将其显示回Angular。我已经完成上传,但是在尝试显示时出现错误。我在POSTMAN中测试了我的API。一切正常。我认为该错误是由于响应类型引起的。

API

    get('/images',(req, res, next) => {
      const user = AuthBase.getUser(req, res);
      this.model.findOne({\[this.userForeignKey\]: new mongoose.Types.ObjectId(user._id)}, (err, image) => {
          if (err) {
              res.sendStatus(400);
          }
          // stream the image back by loading the file
          res.setHeader('Content-Type', 'image/jpeg'); 
          fs.createReadStream(path.join(UPLOAD_PATH, image.filename)).pipe(res);
//if I add res.json(image) here, I am not getting any error but the Image is getting broken        
 })
    });][1] 

服务

getImage(): Observable<any> {
  return this.http.get(`/api/images`);
}

组件

images:[]
  getImage() 
  {
 this.patientService.getImage().subscribe(
    data =>{ 
   this.images = [data]; 
   console.log(data); },
    error => console.log(error) ); 
  }

1 个答案:

答案 0 :(得分:0)

在获取请求中包含响应类型

getImage(): Observable<any> {
  return this.http.get(`/api/images`,{ responseType: 'blob' });
}