0

I have an action method in my vuex store, which has two parameters with names "context", "payload". Payload has data and that vuex action method by itself sees and with console.log() prints in console the data, which comes with that payload parameter, but when I try to send that value with axios to controller, the controller gets empty data. The most terrible thing. If instead of "payload.payloadDataName" I give any other value in that action method, no matter where I give it, it works well, but payload value it sees only out of axios braces. I don't know what the ... sorry, in my brain only rude words, cause I spent more than 8 hours to solve this and no positive result yet. Can you help me to solve this problem?

Here's my code

users.js (vuex store module)

actions: {
        async uploadProfileAvatar(context, payload){
            try {
                const avatar = payload.avatar; //if here i write "const avatar = 8" or "{data:8}" it will work fine 
                const response = await axios.post('/avatar', {data: avatar}); //if here instead of "{data:avatar}" I'll write "{data:8}" or "{data:{help:8}" it will work fine too
                console.log("payload.avatar = ");
                console.log(payload.avatar); //it's strange, but in this line, it works, so I can't say payload parameter's data is not coming
                return response.data;
            } catch (error) {

            }
        }, 
        ...

Profile.vue

<template>
    <div>
        <div class="d-flex justify-content-center">
            <img v-if="src!==''" :src="src" alt="something" width="250px">
        </div>

        <div class="d-flex justify-content-center">
            <form enctype="multipart/form-data">
                <input type="file" v-on:change='formFile' name="img">
                <input type="button" v-on:click="uploadImage()" value="upload">
            </form>
        </div>
        <div>
            Wall
            <button type="button">Hello</button>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Profile",
        data: () => {
            return {
                src: '',
                file: null
            }
        },

        mounted() {
            this.getImage();
        },

        methods: {
            getImage() {

                this.$store.dispatch('users/loadProfileAvatar').then((res) => {
                    console.log('/storage/avatars/avatar-of-user' + this.$userId + '/' + res)
                    this.src = '/storage/avatars/avatar-of-user' + this.$userId + '/' + res;
                }).catch((error) => {
                    console.log(error);
                });
            },

            formFile(e){
                this.file = e.target.files[0];
            },

            uploadImage() {
                console.log(this.file);
                this.$store.dispatch('users/uploadProfileAvatar', {
                    avatar:this.file  //this is the payload's data, which I need. 
                }).then((res) => {
                    console.log(res);
                }).catch((error) => {
                    console.log(error);
                });
            }
        }
    }
</script>

IMPORTATNT! My aim is to get the vue component's template's form input file's data and send it to vuex store module's action method, and from there send it to needed controller by axios.

Updated

async uploadProfileAvatar(context, payload){
            try {
                const avatar = payload.avatar; //if here i write "const avatar = 8" or "{data:8}" it will work fine 
                const response = await axios.post('/avatar', {data: avatar}); //if here instead of "{data:avatar}" I'll write "{data:8}" or "{data:{help:8}" it will work fine too
                console.log("payload.avatar = ");
                console.log(payload.avatar); //it's strange, but in this line, it works, so I can't say payload parameter's data is not coming
                return response.data;
            } catch (error) {

            }
        },

changed to

async uploadProfileAvatar(context, payload){
    try {
        const avatar = payload.avatar;
        let data = new FormData();
        data.append('file',avatar);
        const response = await axios.post('/avatar', data);
        console.log("payload.avatar = ");
        console.log(payload.avatar);
        return response.data;
    } catch (error) {

    }
},

but it not works... interesting, what I don't see?

Updated

Controller's method

public function store(Request $request)
{
    $file = $request->all();
    return response()->json([
        'file' => $file,
    ]);
}
Ruben Kubalyan
  • 288
  • 3
  • 11
  • 1
    https://stackoverflow.com/questions/42907747/how-to-send-a-file-via-axios-to-laravel – Rwd Dec 01 '21 at 18:01
  • @Rwd glad to see you again my friend! Just a moment, I'll try. – Ruben Kubalyan Dec 01 '21 at 18:08
  • @Rwd I updated the question. – Ruben Kubalyan Dec 01 '21 at 18:25
  • 1
    Please can you show your controller code. – Rwd Dec 01 '21 at 18:35
  • @Rwd updated the question – Ruben Kubalyan Dec 01 '21 at 18:51
  • Ah ah, returning an uploaded file is just going to result in `{}` coming back i.e. I would imagine the response is something like `{file: {file: {}}}`. What do you get if you `return ['file' => $request->file('file')->getClientOriginalName()];` from your controller method instead? – Rwd Dec 01 '21 at 19:04
  • @Rwd "{file: {file: {}}}" Yes. You imagine correct. That's nearly the same result that I get. But I didn't understand why returning the uploaded file result in {}. One minute, I'll try to "return ['file' => $request->file('file')->getClientOriginalName()];" – Ruben Kubalyan Dec 01 '21 at 19:17
  • @Rwd interesting... very interesting. It works with "->getClientOriginalName()", "->getSize()", "->getClientOriginalExtension()" – Ruben Kubalyan Dec 01 '21 at 19:27
  • 1
    In that case the file upload is working fine :) – Rwd Dec 01 '21 at 19:30
  • @Rwd That's great. It's very funny, cause as I see I solved this problem many hours ago, but the version that the problem is "returning the uploaded file itself back to client side can make an illusion of problem" .... hell....)))) That's really funny. Anyway. Many thanks to you my friend! You're great as always! – Ruben Kubalyan Dec 01 '21 at 19:37

1 Answers1

0

Try by wrapping context with curly braces such as

async uploadProfileAvatar({context}, payload){
...
}
Luciano
  • 2,052
  • 1
  • 16
  • 26