Rui Inacio
Rui Inacio

Reputation: 25

API returning empty array instead of data in Laravel Vue

I am doing a Laravel Vue project for school and i am supposed to get a user by sending its email from the client, but when the server responds i get an empty array from the response data instead of the data i want from the database.

Login.vue

login() {

        this.showMessage = true;
        this.typeofmsg = "alert";
        this.message = "Loggin in...";

        axios.post('api/login', this.user)
            .then(response => {
                const token = response.data.access_token;
                this.$store.commit('setAccessToken', token);
                return axios.get('api/users/me', this.user.email);
            })
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.log("Error = " + error.message);
            });

    },

routes/api.js

Route::get('users/me', 'UserControllerAPI@myProfile');

UserControllerAPI

public function myProfile(Request $request){
    $email = $request->email;
    $user = User::where('email', $email)->first();
    return new UserResource($user);
}

If i try to get it with postman it works enter image description here

And in the dev tools console i get this

enter image description here

Sorry if i wasn't clear enought or it is something wrongly made, i have been trying to fix this since yesterday and its driving me crazy. Any help appreciated

Edit: Had the route wrong, but i changed it and i get the same, no data. I changed the console picture too

Upvotes: 0

Views: 799

Answers (2)

Kwame Opare Asiedu
Kwame Opare Asiedu

Reputation: 2345

Change this line:

return axios.get('api/users/me', this.user.email);

to

return axios.get('api/users/me', { params: { email: this.user.email } });

Upvotes: 2

MR.GEWA
MR.GEWA

Reputation: 873

You have api/user/me and in route you have /userS/me

so I guess you have to either remove S or add S in one of the places.

so try to change in route

Route::get('users/me', 'UserControllerAPI@myProfile');

to

Route::get('/user/me', 'UserControllerAPI@myProfile');

Upvotes: 0

Related Questions