Assign the return of a function that returns from a Promise

Asked

Viewed 891 times

2

I am exporting a function to another file that is as a Webpack-simple + Vuejs component. That’s the function of the file api-user.js:

export function getUsers () {
    axios.post('/api/get_list',{})
    .then(req => {return req.data.list;})
    .catch(err => console.log(err))
}

In the archive myComponent.Vue, I’m importing her and calling her that:

created: async function () {
      this.arrayUsers = await getUsers()
      console.log(this.arrayUsers)

},

but I only get paid undefined, and I’m not really understanding why.

An example of what I’m trying to do:

https://codesandbox.io/s/n046mxwpm


EDIT:

I got it in a way here, but it’s not what I want yet, because it requires a lot of code in the file which I’m trying to keep cleaner.

api-user.js:

export function getUsers () { 
    return axios.post('/apiminer/get_list',{}) 
} 

mycomponent.Vue:

created: function() { 

    getUsers().then((res) => { 
         this.arrayUsers= res.data.list 
    }) 
},

2 answers

0

The API method returns a Promise and you’re returning this Promise into the function getUsers().

The function getUsers() should also return a Promise being like this:

getUsers(){
  return new Promise((resolve, reject) => {
    api.post(url).then(({ data }) => resolve(data))
  });
}

0

I can’t tell you exactly what mistake might be going on, but I’ll try to help based on what you reported.

The first mistake I see is that you have a method getUsers(), but within the method you are performing a post, in the case axios.post. In this case there were to be more errors, such as 405 - Method not Allowed, with the exception of returning undefined.

Another problem that may happen is that in Vuejs, I particularly import functions like this from other files to use them like Filters, directly on your tag <template>, and not to call functions within the created().

May I suggest you try to change this function to be used as a mixin, and so it may help you solve the problem. See below:

api-users.js

export default {
  methods: {
    getUsers () {
      axios.post('/api/get_list',{})
        .then(req => {return req.data.list;})
        .catch(err => console.log(err))
    }
  }
}

myComponent.Vue

In his <template>, insert:

<tr v-for="(item, index) in arrayUsers" v-if="arrayUsers && arrayUsers.length">

Below is the code within the <script>:

import apiUser from "./api-user.js";

export default {
  name: "HelloWorld",
  mixins: [apiUser],
  data() {
    return {
      arrayUsers: []
    };
  },
  created() {
    this.arrayUsers = this.getUsers();
  },      
};

Remarks:

  1. Fix the import directory for mixin apiUsers;
  2. Check if your configuration in your main.js or other configuration file such as port, etc., is correct;
  3. Check that the method name in the back end is correct, as well as its type and the return of the api, if it is actually being returned in an array of objects list, within the date. Perform some kind of automated testing or test it by Postman.

I hope I’ve helped.

  • The api is okay, I’ve tested it. I did not understand why it was supposed to occur "405 - Method not Allowed", because if I do the same in the component file everything works, the problem is trying to organize the code in two separate files. I’ve tried mixins and it didn’t work. The problem I see (I’m not sure) is that I’m returning from a method that has a promisse, and since it’s asynchronous, it’s not very sure when it will assign the value to the arrayUsers variable, which can happen to receive Undefined, but vuejs is reactive, so I guess that’s not it, I have no idea what to try.

  • Just to comment, for some reason my internet does not want to open the link sent in the codesandbox. If you’re passing information that doesn’t help, let me know that I delete the answer.

  • opened here on another computer and went, test there again the link.

  • @Perdugames Tried exactly with the code I passed and it didn’t work? Strange indeed. About the 405, usually in API’s Rest, in the back end to bring data, in case users, it is set that will be performed a GET. Soon, on your front end should be done a GET, otherwise usually it will return to 405. In case it is asynchronous or not, at the time it receives the return it will assign the return to its date variable.

  • Not that, with the API is all ok, in the example of the codesandbox I use calling for get, but this internal here is post same, but it is ok, everything tested already. The problem is in some concept ai of javascript or vuejs that I still do not understand. And I tried yes, actually it was the first attempt I made was with mixins.

  • I checked the phone for the post and edited the answer, see if it helps anything. Although the code of the codesandbox is a little different from what I’ve seen.

Show 2 more comments

Browser other questions tagged

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