无法使用multipart / formdata更新实体-NgRx数据

问题描述 投票:0回答:1

我正在尝试更新一个具有文件上传功能的实体。我可以使用add方法发送(添加)FormData,但无法更新。 NgRx给出以下错误:

错误:主键不能为空/未定义。

这甚至可能吗?还是我做错了什么。请看下面的代码:

const ad = {
  id: form.id,
  accountId: this.accountId
}

const data = new FormData();
data.append('ad', JSON.stringify(ad));

// photos is an array of uploaded files
if(this.photos.length) {
  this.photos.forEach(photo => {
    data.append('offure_ad', photo, photo['name']);
  });
}

// NgRx Data update mothod
this.adService.update(data);

请引导我正确的方向。谢谢

angular ngrx ngrx-data
1个回答
0
投票

请尝试以下代码

const ad = {
  id: form.id,
  accountId: this.accountId
}

const data = new FormData();
data.append('ad', JSON.stringify(ad));

// photos is an array of uploaded files
if(this.photos.length) {
  this.photos.forEach(photo => {
    data.append('offure_ad', photo, photo['name']);
  });
}

// send ad like below
this.adService.update(data,ad);
© www.soinside.com 2019 - 2024. All rights reserved.