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
})
},
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.
– PerduGames
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.
– guastallaigor
opened here on another computer and went, test there again the link.
– PerduGames
@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.
– guastallaigor
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.
– PerduGames
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.
– guastallaigor
Let’s go continue this discussion in chat.
– guastallaigor