Vue receiving data from the server









up vote
0
down vote

favorite












Just want to say that I am became familiar with vue not so long ago.

Now I am having a problem.



I fetch data from the server.



// store

import PostService from '../services/PostService'



export default 
state:
posts: ,

,
actions:
async getPost()
const response = await PostService.fetchPosts();
console.log(response.data.posts);

this.posts = response.data.posts






There is an array for data and a request to the server.
In response, the data comes.



// vue component 
<template>
<section class="posts--wrap">
<div class="posts wrapper">
<h1>Posts</h1>
<div
class="posts__item--wrap"
v-if="this.allPosts.length"
>
<h3>List of posts</h3>
<div
v-for="(post, index) in allPosts"
:key="index"
class="posts__item">
<h3 class="posts__item-title">post.title</h3>
<p class="posts__item-text">post.description</p>
</div>
</div>
<div
v-else
class="posts__item-error"
>
<h3>There are no posts ... Lets add one now!</h3>
<router-link tag="div" :to="name: 'Add'">
<a>Add new post ;)</a>
</router-link>
</div>
</div>
</section>
</template>

<script>
import mapState from 'vuex';
import mapActions from 'vuex';

export default
name: 'PostPage',
data ()
return


,
computed: mapState(
allPosts: state => state.posts.posts
),
methods:
...mapActions(
getAllPosts: 'getPost'
)
,
mounted()
console.log(this.allPosts);
this.getAllPosts();


</script>


If add something to state.posts, it will be displayed on the page.



But I can't figure out how to get the data from the response into the posts
I ask for help, or hints.
Thank!



UPD
Made changes for which displays the response



response.log



(2) […, …]
0: _id: "5be4bbfaad18f91fbf732d17", title: "Title post 1", description: "esxdfgdfghj"
1: _id: "5be490f930fba81a704867f6", title: "2312", description: "312312312"
length: 2
__proto__: Array(0)









share|improve this question























  • can we see what the console.log of the response shows please.
    – Andrew1325
    Nov 9 at 21:56










  • @Andrew1325 I updated the question
    – Skip Tyler
    Nov 9 at 22:21










  • Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
    – Nikolay Vetrov
    Nov 9 at 23:32














up vote
0
down vote

favorite












Just want to say that I am became familiar with vue not so long ago.

Now I am having a problem.



I fetch data from the server.



// store

import PostService from '../services/PostService'



export default 
state:
posts: ,

,
actions:
async getPost()
const response = await PostService.fetchPosts();
console.log(response.data.posts);

this.posts = response.data.posts






There is an array for data and a request to the server.
In response, the data comes.



// vue component 
<template>
<section class="posts--wrap">
<div class="posts wrapper">
<h1>Posts</h1>
<div
class="posts__item--wrap"
v-if="this.allPosts.length"
>
<h3>List of posts</h3>
<div
v-for="(post, index) in allPosts"
:key="index"
class="posts__item">
<h3 class="posts__item-title">post.title</h3>
<p class="posts__item-text">post.description</p>
</div>
</div>
<div
v-else
class="posts__item-error"
>
<h3>There are no posts ... Lets add one now!</h3>
<router-link tag="div" :to="name: 'Add'">
<a>Add new post ;)</a>
</router-link>
</div>
</div>
</section>
</template>

<script>
import mapState from 'vuex';
import mapActions from 'vuex';

export default
name: 'PostPage',
data ()
return


,
computed: mapState(
allPosts: state => state.posts.posts
),
methods:
...mapActions(
getAllPosts: 'getPost'
)
,
mounted()
console.log(this.allPosts);
this.getAllPosts();


</script>


If add something to state.posts, it will be displayed on the page.



But I can't figure out how to get the data from the response into the posts
I ask for help, or hints.
Thank!



UPD
Made changes for which displays the response



response.log



(2) […, …]
0: _id: "5be4bbfaad18f91fbf732d17", title: "Title post 1", description: "esxdfgdfghj"
1: _id: "5be490f930fba81a704867f6", title: "2312", description: "312312312"
length: 2
__proto__: Array(0)









share|improve this question























  • can we see what the console.log of the response shows please.
    – Andrew1325
    Nov 9 at 21:56










  • @Andrew1325 I updated the question
    – Skip Tyler
    Nov 9 at 22:21










  • Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
    – Nikolay Vetrov
    Nov 9 at 23:32












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Just want to say that I am became familiar with vue not so long ago.

Now I am having a problem.



I fetch data from the server.



// store

import PostService from '../services/PostService'



export default 
state:
posts: ,

,
actions:
async getPost()
const response = await PostService.fetchPosts();
console.log(response.data.posts);

this.posts = response.data.posts






There is an array for data and a request to the server.
In response, the data comes.



// vue component 
<template>
<section class="posts--wrap">
<div class="posts wrapper">
<h1>Posts</h1>
<div
class="posts__item--wrap"
v-if="this.allPosts.length"
>
<h3>List of posts</h3>
<div
v-for="(post, index) in allPosts"
:key="index"
class="posts__item">
<h3 class="posts__item-title">post.title</h3>
<p class="posts__item-text">post.description</p>
</div>
</div>
<div
v-else
class="posts__item-error"
>
<h3>There are no posts ... Lets add one now!</h3>
<router-link tag="div" :to="name: 'Add'">
<a>Add new post ;)</a>
</router-link>
</div>
</div>
</section>
</template>

<script>
import mapState from 'vuex';
import mapActions from 'vuex';

export default
name: 'PostPage',
data ()
return


,
computed: mapState(
allPosts: state => state.posts.posts
),
methods:
...mapActions(
getAllPosts: 'getPost'
)
,
mounted()
console.log(this.allPosts);
this.getAllPosts();


</script>


If add something to state.posts, it will be displayed on the page.



But I can't figure out how to get the data from the response into the posts
I ask for help, or hints.
Thank!



UPD
Made changes for which displays the response



response.log



(2) […, …]
0: _id: "5be4bbfaad18f91fbf732d17", title: "Title post 1", description: "esxdfgdfghj"
1: _id: "5be490f930fba81a704867f6", title: "2312", description: "312312312"
length: 2
__proto__: Array(0)









share|improve this question















Just want to say that I am became familiar with vue not so long ago.

Now I am having a problem.



I fetch data from the server.



// store

import PostService from '../services/PostService'



export default 
state:
posts: ,

,
actions:
async getPost()
const response = await PostService.fetchPosts();
console.log(response.data.posts);

this.posts = response.data.posts






There is an array for data and a request to the server.
In response, the data comes.



// vue component 
<template>
<section class="posts--wrap">
<div class="posts wrapper">
<h1>Posts</h1>
<div
class="posts__item--wrap"
v-if="this.allPosts.length"
>
<h3>List of posts</h3>
<div
v-for="(post, index) in allPosts"
:key="index"
class="posts__item">
<h3 class="posts__item-title">post.title</h3>
<p class="posts__item-text">post.description</p>
</div>
</div>
<div
v-else
class="posts__item-error"
>
<h3>There are no posts ... Lets add one now!</h3>
<router-link tag="div" :to="name: 'Add'">
<a>Add new post ;)</a>
</router-link>
</div>
</div>
</section>
</template>

<script>
import mapState from 'vuex';
import mapActions from 'vuex';

export default
name: 'PostPage',
data ()
return


,
computed: mapState(
allPosts: state => state.posts.posts
),
methods:
...mapActions(
getAllPosts: 'getPost'
)
,
mounted()
console.log(this.allPosts);
this.getAllPosts();


</script>


If add something to state.posts, it will be displayed on the page.



But I can't figure out how to get the data from the response into the posts
I ask for help, or hints.
Thank!



UPD
Made changes for which displays the response



response.log



(2) […, …]
0: _id: "5be4bbfaad18f91fbf732d17", title: "Title post 1", description: "esxdfgdfghj"
1: _id: "5be490f930fba81a704867f6", title: "2312", description: "312312312"
length: 2
__proto__: Array(0)






vue.js async-await store vuex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 22:21

























asked Nov 9 at 21:26









Skip Tyler

237




237











  • can we see what the console.log of the response shows please.
    – Andrew1325
    Nov 9 at 21:56










  • @Andrew1325 I updated the question
    – Skip Tyler
    Nov 9 at 22:21










  • Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
    – Nikolay Vetrov
    Nov 9 at 23:32
















  • can we see what the console.log of the response shows please.
    – Andrew1325
    Nov 9 at 21:56










  • @Andrew1325 I updated the question
    – Skip Tyler
    Nov 9 at 22:21










  • Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
    – Nikolay Vetrov
    Nov 9 at 23:32















can we see what the console.log of the response shows please.
– Andrew1325
Nov 9 at 21:56




can we see what the console.log of the response shows please.
– Andrew1325
Nov 9 at 21:56












@Andrew1325 I updated the question
– Skip Tyler
Nov 9 at 22:21




@Andrew1325 I updated the question
– Skip Tyler
Nov 9 at 22:21












Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
– Nikolay Vetrov
Nov 9 at 23:32




Your response object doesn't have a property named data. Take a look at API, I mean you have to know the structure of the response object.
– Nikolay Vetrov
Nov 9 at 23:32












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You did wrong in your action getPost, the proper way to change a state through an action is to commit a mutation using action context parameter like this:



...
mutations:
setPosts(state, posts)
state.posts = posts;
,
,
actions:
async getPost(context)
/* ... */
context.commit('setPosts', response.data.posts);

,
...


Read more about Vuex actions.






share|improve this answer




















  • Thank you! I will read the documentation!
    – Skip Tyler
    Nov 10 at 21:29










  • You’re welcome.
    – Madmadi
    Nov 11 at 8:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233472%2fvue-receiving-data-from-the-server%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You did wrong in your action getPost, the proper way to change a state through an action is to commit a mutation using action context parameter like this:



...
mutations:
setPosts(state, posts)
state.posts = posts;
,
,
actions:
async getPost(context)
/* ... */
context.commit('setPosts', response.data.posts);

,
...


Read more about Vuex actions.






share|improve this answer




















  • Thank you! I will read the documentation!
    – Skip Tyler
    Nov 10 at 21:29










  • You’re welcome.
    – Madmadi
    Nov 11 at 8:41














up vote
1
down vote



accepted










You did wrong in your action getPost, the proper way to change a state through an action is to commit a mutation using action context parameter like this:



...
mutations:
setPosts(state, posts)
state.posts = posts;
,
,
actions:
async getPost(context)
/* ... */
context.commit('setPosts', response.data.posts);

,
...


Read more about Vuex actions.






share|improve this answer




















  • Thank you! I will read the documentation!
    – Skip Tyler
    Nov 10 at 21:29










  • You’re welcome.
    – Madmadi
    Nov 11 at 8:41












up vote
1
down vote



accepted







up vote
1
down vote



accepted






You did wrong in your action getPost, the proper way to change a state through an action is to commit a mutation using action context parameter like this:



...
mutations:
setPosts(state, posts)
state.posts = posts;
,
,
actions:
async getPost(context)
/* ... */
context.commit('setPosts', response.data.posts);

,
...


Read more about Vuex actions.






share|improve this answer












You did wrong in your action getPost, the proper way to change a state through an action is to commit a mutation using action context parameter like this:



...
mutations:
setPosts(state, posts)
state.posts = posts;
,
,
actions:
async getPost(context)
/* ... */
context.commit('setPosts', response.data.posts);

,
...


Read more about Vuex actions.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 23:29









Madmadi

966




966











  • Thank you! I will read the documentation!
    – Skip Tyler
    Nov 10 at 21:29










  • You’re welcome.
    – Madmadi
    Nov 11 at 8:41
















  • Thank you! I will read the documentation!
    – Skip Tyler
    Nov 10 at 21:29










  • You’re welcome.
    – Madmadi
    Nov 11 at 8:41















Thank you! I will read the documentation!
– Skip Tyler
Nov 10 at 21:29




Thank you! I will read the documentation!
– Skip Tyler
Nov 10 at 21:29












You’re welcome.
– Madmadi
Nov 11 at 8:41




You’re welcome.
– Madmadi
Nov 11 at 8:41

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233472%2fvue-receiving-data-from-the-server%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo