Next.js Discord

Discord Forum

How do I prevent axios from altering data structure

Unanswered
Bonga shad posted this in #help-forum
Open in Discord
Bonga shadOP
I am working with data from an api end that returns data in the structure of an object as text and looks like this
{
  "Users": [
    {
      "UserId": 1,
      "UserName": "User
      "age": 50,
      "LogData": [
        {
          "Timestamp": "2024-01-01T08:00:00",
          "Place": "London
        }
      ]
    }
  ]
}


But when converted, it looks like this
{
  "Buildings": [
    0: {
      "User": 1,
      "User": "User 1",
      "age": 50,
      "LogData": 
        {
          "Timestamp": "2024-01-01T08:00:00",
          "Place": "London
        }
      ]
    },
1: {
      "User": 2,
      "User": "User 2",
      "age": 50,
      "LogData": 
        {
          "Timestamp": "2024-01-01T08:00:00",
          "Place": "Paris"
        }
      ]
    }
  ]
}


My question is, how do I precent this from happening?

This is my fetch code

export interface FetchResponse<T> {
  count: number;
  next: string | null;
  results: T[];
}

class APIClient<T> {
  baseURL: string;
  endpoint: string;

  constructor(endpoint: string) {
    this.baseURL = process.ENV.API_URL;
    this.endpoint = endpoint;
  }

  get = async (
    startdate: number | string,
    enddate: number | string
  ): Promise<FetchResponse<T>> => {
    const response = await fetch(
      `${this.baseURL}/${this.endpoint}`
    );

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const responseData = await response.json();
    return responseData as FetchResponse<T>;
  };
}

export default APIClient;


This gives the same response as using axios

0 Replies