Isabella Monza
Isabella Monza

Reputation: 159

Wrap a request in an array

I have such a problem, when I try to send a PayLoad to the server (PUT), I get an error because my object is not in the array results:[]

Are there ways to wrap an object inside an array with a specific name?

At the moment my request looks like this:

enter image description here

The server requires for PUT that the object be wrapped in an array "results:[]", like this:

enter image description here

  this.service.putNewsData(this.data).subscribe(res => {
    this.service.service.notifySuccess();
    this.ngOnInit();
  }, err => {
    this.service.service.notifyError();
    this.loadComplete = true;
});

Where this.data ->

(6) [{…}, {…}, {…}, {…}, {…}, {…}]

I am looking for a way to make it like this ->

results: (6) [{…}, {…}, {…}, {…}, {…}, {…}]

Thanks for helping!

Upvotes: 0

Views: 434

Answers (2)

Gee
Gee

Reputation: 1418

Unless I totally misunderstood your question, you just need to wrap your array in an object like this:

this.service.putNewsData({results: this.data}).subscribe(res => {

Upvotes: 0

Gaël J
Gaël J

Reputation: 15105

You're just looking for wrapping your array in an object:

const dataToSend = { results: this.data }

Upvotes: 1

Related Questions