0

I want to know how can we create const object variable with array, in which when we add item it should not contain index object.

e.g.

const estimateData = {
  'customerId': customerId,
  'vehicleInformation': {},
  'datalines': []
};

Here, When I add items in datalines, it always comes with key-value pair like below.

  "datalines": [
    0 : {
      "lineNumber": 1,
      ...
    },
    1 : {
      "lineNumber": 2,
      ...
    }
  ]

Because of this my webservice returns exception with error like

error: "Bad Request"
exception: "com.alldata.estimator.exceptions.BadRequestException"
message: "unknown dataline type 0"
path: "/estimator/estimates/211204"

So my question is how can I remove that 0,1 indexes so my request can be passed successfully and data can be approved. I have tried to add data via push, reassigning array, forEach loop but nothing worked.

  // I am trying to add data in array by following methods, but nothing works. 
  this.selectedEstimate.datalines.forEach((lineItem: ILineItem) => {
    estimateData.datalines.push(lineItem);
  });
  // estimateData.datalines = this.selectedEstimate.datalines;

I have also tried to parse data from JSON, but not worked.

Please let me know where is the issue and how can I solve this. Thanks in Advance.

0

1 Answer 1

1

Stringify your estimateData before sending to the server.

Use the below code:-

JSON.stringify(estimateData)

const a = {
  'customerId': 1,
  'vehicleInformation': {},
  'datalines': []
};
a.datalines.push({age: 1});
a.datalines.push({age: 2});
a.datalines.push({age: 3});
a.datalines.push({age: 4});

// open browser console to check response
console.log(a);
console.log(JSON.stringify(a));

Please open the browser console to look at the indexes getting printed.

Sign up to request clarification or add additional context in comments.

3 Comments

I already have tried. Stringify is not an option. webservice will only accept json.
Send Content-Type as application/json in the headers of your request and the data in stringified format.
Create a stackblitz then. And yes, the data travels as string over the internet and you have to specify the Content-Type in the headers to tell the format of the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.