problems with Axios and React-Native. Axios returning empty attributes

Asked

Viewed 112 times

-1

All right?

I am developing an application for a client in React-On, due to some integrations that the app needs to have I developed his backend using Laravel 6.x. So far so good, I’ve done all the endpoints and tested them all.

But when you access a certain endpoint, which is the one that brings the data of a product, one of the attributes of that object, which is an array, always comes empty. And I can’t find the reason. Someone’s been there?

This here is the method that will return the data.

public function recover($product)
    {
        $product = Product::with(['skus', 'gallery', 'categories'])->find($product);

        return response()->json($product);
    }

That would be the comeback when I test the endpoint on Postman.

{
"id": "453449b4-6311-4237-a88b-06b1ca41c281",
"title": "...",
"short_description": "...",
"description": "..."
"brand": "Probiotica",
"weight": 2.62,
"height": 30,
"width": 20,
"length": 20,
"tags": null,
"parent": "...",
"visible": 1,
"created_at": "2019-11-25 11:46:00",
"updated_at": "2020-01-06 12:13:54",
"deleted_at": null,
"skus": [
    {
        "id": "a0bc2293-df1d-4c57-8148-19d0085c3aaf",
        "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
        "title": "...",
        "price": "188.90",
        "discounted_price": "188.90",
        "stock": 4,
        "minimum_stock": 0,
        "created_at": "2019-11-25 11:46:00",
        "updated_at": "2020-01-06 12:13:54"
    }
],
"gallery": [
    {
        "id": "5ad17751-1b9a-43f1-b896-5ba97f4e23cb",
        "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
        "image": "...",
        "subtitle": null,
        "created_at": "2020-01-06 12:13:54",
        "updated_at": "2020-01-06 12:13:54"
    }
],
"categories": [
    {
        "id": "7b4eaa17-4a98-4f70-adfc-2c71cf0937d0",
        "parent": 0,
        "title": "MASSA MUSCULAR",
        "icon": "categories/7b4eaa17-4a98-4f70-adfc-2c71cf0937d0.jpeg",
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-12-16 14:05:18",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "7b4eaa17-4a98-4f70-adfc-2c71cf0937d0"
        }
    },
    {
        "id": "bd5cec08-9678-4f71-9ec2-38b2d02e6cf5",
        "parent": 1320136,
        "title": "Proteínas",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "bd5cec08-9678-4f71-9ec2-38b2d02e6cf5"
        }
    },
    {
        "id": "0c2bc1ab-29a3-4cf2-8ac7-a72e204932d1",
        "parent": 1320154,
        "title": "Whey Protein",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "0c2bc1ab-29a3-4cf2-8ac7-a72e204932d1"
        }
    },
    {
        "id": "dda4544b-8b57-4e80-aea1-bdde57fc9972",
        "parent": 1320160,
        "title": "100% Concentrada",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "dda4544b-8b57-4e80-aea1-bdde57fc9972"
        }
    }
]

}

And this would be the object that is returned by the Axios GET within the application. Note that only the attribute skus that comes empty.

{
"id": "453449b4-6311-4237-a88b-06b1ca41c281",
"title": "...",
"short_description": "...",
"description": "..."
"brand": "Probiotica",
"weight": 2.62,
"height": 30,
"width": 20,
"length": 20,
"tags": null,
"parent": "...",
"visible": 1,
"created_at": "2019-11-25 11:46:00",
"updated_at": "2020-01-06 12:13:54",
"deleted_at": null,
"skus": [],
"gallery": [
    {
        "id": "5ad17751-1b9a-43f1-b896-5ba97f4e23cb",
        "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
        "image": "...",
        "subtitle": null,
        "created_at": "2020-01-06 12:13:54",
        "updated_at": "2020-01-06 12:13:54"
    }
],
"categories": [
    {
        "id": "7b4eaa17-4a98-4f70-adfc-2c71cf0937d0",
        "parent": 0,
        "title": "MASSA MUSCULAR",
        "icon": "categories/7b4eaa17-4a98-4f70-adfc-2c71cf0937d0.jpeg",
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-12-16 14:05:18",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "7b4eaa17-4a98-4f70-adfc-2c71cf0937d0"
        }
    },
    {
        "id": "bd5cec08-9678-4f71-9ec2-38b2d02e6cf5",
        "parent": 1320136,
        "title": "Proteínas",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "bd5cec08-9678-4f71-9ec2-38b2d02e6cf5"
        }
    },
    {
        "id": "0c2bc1ab-29a3-4cf2-8ac7-a72e204932d1",
        "parent": 1320154,
        "title": "Whey Protein",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "0c2bc1ab-29a3-4cf2-8ac7-a72e204932d1"
        }
    },
    {
        "id": "dda4544b-8b57-4e80-aea1-bdde57fc9972",
        "parent": 1320160,
        "title": "100% Concentrada",
        "icon": null,
        "visible": 1,
        "created_at": "2019-11-25 11:52:59",
        "updated_at": "2019-11-25 11:52:59",
        "pivot": {
            "product_id": "453449b4-6311-4237-a88b-06b1ca41c281",
            "category_id": "dda4544b-8b57-4e80-aea1-bdde57fc9972"
        }
    }
]

}

This is the action I use to retrieve products.

Axios.get(`${endpoint}/product/${id}`, { headers: { Accept: 'application/json' } })
    .then(({ data }) => {
      dispatch({
        type: Types.FETCH_PRODUCT,
        payload: data
      });
    });

What could possibly be going on to make this attribute empty? Ah, a note, if I remove the attributes gallery and categories then he will return me the skus usually... the.O

  • Local problems are hard to know.

  • how so local problems? You say in localhost? Because if so the problem is in both locations, both localhost and homologation...

  • Is not localhost in case I am referring that this is happening in your project and it is difficult for us to guess why this is happening.

1 answer

0


The problem was solved, in fact, what was happening is the following, when recovering this data, inside the component in which I called the action I was, unintentionally, making the mutation of the same, and so was in the store the value as null, and I had given a .pop() in the array in question...

Anyway, a silly mistake that cost me a ton of time. hehehehehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.