简体   繁体   中英

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在此处输入图片说明

And in the dev tools console i get this

在此处输入图片说明

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

Change this line:

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

to

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM