单击(http.put)后在Json上创建一个新对象

时间:2017-04-07 14:46:27

标签: javascript json angular

我有以下json结构:

"disputas": [
    {
      id: "",
      tipo_negociacao: "",
      historico:{
         fl_usuario: "",
         created_at: "",
         updated_at: "",
         created_by: null,
         updated_by: null,
         texto: "",
      }
    }
]

我正在使用http.put更新此json,我想在每次单击按钮时创建另一个historico对象,所以它看起来像这样:

"disputas": [
    {
      id: "",
      tipo_negociacao: "",
      historico:{
         fl_usuario: "",
         created_at: "",
         updated_at: "",
         created_by: null,
         updated_by: null,
         texto: "",
      },
         historico:{
         fl_usuario: "",
         created_at: "",
         updated_at: "",
         created_by: null,
         updated_by: null,
         texto: "",
      }
    }
]

我该怎么做?

这是我的服务:

url: string = 'http://localhost:3004/disputa';
constructor(private http: Http){}
atualizaDisputa (body:Object): Observable<DisputaPropostaComponent[]>{
    let bodyString = JSON.stringify(body); // Stringify payload
    let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
    let options       = new RequestOptions({ headers: headers }); // Create a request option
    return this.http.put(`${this.url}/${body['id']}`, body, options) // ...using post request
                     .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
                     .catch((error:any) => Observable.throw(error.json().error || 'Ocorreu um erro em nosso servidor, tente novamente mais tarde')); //...errors if any
}

提前致谢。如果你们需要更多代码,请告诉我。

1 个答案:

答案 0 :(得分:1)

你不能在一个对象上有两个名为historico的键。您需要将其转换为数组。一旦你这样做,你可以像往常一样推进阵列。