1

I am having an odd issue where my Laravel API is returning JSON data to my react front-end via Axios and then Axios is parsing the data and returning an undefined value for a specific key "file".

Ive added a transformResponse function to the Axios get method that logs the response before and after doing my own parsing and I still get the same result. Im also using the reviver parameter to log the value when the key is file.

api.get(`xxxxx`, {
            transformResponse: (res) => {
                console.log(res);

                const  data = JSON.parse(res, (key,value) => {
                    if (key === 'file' && value !== null) console.log(value);
        
                    return value;
                });

                console.log(data);

                return data;
            },
        }).then(res => {

The output of the first log:

{"data":{"value":{"value": 20,"users": [],"file":{"id":20,"fileable_id":2,"folder_id":21,"group_id":null,"name":"IMG_6274.jpg","size":4263000,"downloads":1,"created_at":"2023-11-01T17:24:17.000000Z","updated_at":"2023-11-01T17:24:29.000000Z","deleted_at":null,"extension":"jpg","size_formatted":"4.07 MB","full_url":"https:\/\/ca-dev-stor.s3.us-east-2.amazonaws.com\/club\/2\/fields\/38\/2b837cdd-ee95-49a9-992d-74211b4400da.jpg"}}}}

The output of the second log:

{
    "id": 20,
    "fileable_id": 2,
    "folder_id": 21,
    "group_id": null,
    "name": "IMG_6274.jpg",
    "size": 4263000,
    "downloads": 1,
    "created_at": "2023-11-01T17:24:17.000000Z",
    "updated_at": "2023-11-01T17:24:29.000000Z",
    "deleted_at": null,
    "extension": "jpg",
    "size_formatted": "4.07 MB",
    "full_url": "https://ca-dev-stor.s3.us-east-2.amazonaws.com/club/2/fields/38/2b837cdd-ee95-49a9-992d-74211b4400da.jpg"
}

And the output of the third log:

{
    "data": {
        "value": {
            "value": 20,
            "users": [],
            "file": undefined
        }
    }
}

However if i take the original line and parse it as a string instead of from the response object i get no issue. Not sure how to fully reproduce this - mostly hoping someone has run into this issue before. Thanks!

2
  • I'm not able to replicate the issue. If you are also not able to replicate it then this pretty strongly suggests that the issue isn't what/where you think it is. Is this operation being invoked twice and you're looking at the wrong output? How is ReactJS involved at all? Without an example demonstrating the problem, there's likely not much we can do. Commented Nov 1, 2023 at 20:01
  • Thanks for the response @David. I have tried to produce a fiddle replicating the issue but can't seem to reproduce it. ReactJS tag is provided for context of the JS environment where this is occurring. I agree with your theory although unfortunately I can't provide the larger picture here for deeper inspection. Commented Nov 8, 2023 at 19:18

0

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.