fetch() function is not working in methods - laravel

I am trying to retrieve data from REST API. I got below code from a tutorial which are not working in my machine but working in that video.
<template>
<div>
<h2>Articles</h2>
<div class="card mb-2" v-for="article in articles" v-bind:key="article.id">
<div class="card-header">
{{ article.title }}
</div>
<div class="card-body">
{{ article.body }}
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
articles : [],
article : {
id : '',
title : '',
body : ''
},
article_id : '',
pagination : {},
edit : false
}
},
created(){
this.fetchArticles();
},
methods:{
fetchArticles(){
fetch('api/articles')
.then(res => res.json())
.then(res => {
this.arcticles = res.data;
console.log(res.data);
})
}
}
}
</script>
After few research I changed my code to
<template>
<div>
<h2>Articles</h2>
<div class="card mb-2" v-for="article in articles" v-bind:key="article.id">
<div class="card-header">
{{ article.title }}
</div>
<div class="card-body">
{{ article.body }}
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
articles : [],
article : {
id : '',
title : '',
body : ''
},
article_id : '',
pagination : {},
edit : false
}
},
created(){
axios
.get('api/articles')
.then(res => {
this.articles = res.data.data;
console.log(res.data.data);
})
},
}
</script>
and it's working fine.
I want to fix the previous code (from the tutorial). Otherwise I'll not get further lessons.

From MDN documentation, the json() method is implemented on Response body, and it returns a promise wich resolves to result of parsing the body text as json.
response.json().then(data => {
// do something with your data
});
So, when using in fetch, the correct syntax is:
fetchArticles(){
fetch('api/articles')
.then(res => res.json())
.then(data=> {
this.arcticles = data; // the result here contains only the json data, it is not a response object, so it does not have a 'data' property.
console.log(data);
})
}

Related

How do I get the images to show in divs, not only the link. Nuxt, CocktailDB API

Im building a simple app to practise Nuxt and axios with the CocktailDB API https://www.thecocktaildb.com/api.php. I have troubble getting the images to show rather than just the links. In console.log the links also shows. How do I get the images from the API to show in the list, and not only the link? Thanks in advance!
<template>
<div>
<div>
<SearchDrink/>
</div>
<div>
<div v-for="drink in drinks" :key="drink.id">
<div class="drink">
<p> {{ drink.strDrink
}} </p>
<img src="" alt=""/> {{ drink.strDrinkThumb
}}
<p>Instructions:</p>
<p> {{ drink.strInstructions }} </p>
<div class="ing"> Ingridients:
<p> {{ drink.strIngredient1 }} </p>
<p> {{ drink.strIngredient2 }} </p>
<p> {{ drink.strIngredient3 }} </p>
<p> {{ drink.strIngredient4 }} </p>
<p> {{ drink.strIngredient5 }} </p>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import SearchDrink from '../../components/SearchDrink.vue'
import axios from 'axios'
export default {
components:{
SearchDrink,
},
data(){
return {
drinks: [],
}
},
methods: {
getAllDrinks(){
axios.get('https://thecocktaildb.com/api/json/v1/1/search.php?s=')
.then((response) => {
this.drinks = response.data.drinks
const myDrink = response.data.drinks
console.log(myDrink)
console.log(myDrink.strDrinkThumb)
})
.catch((error) =>{
console.log(error)
})
},
},
created(){
this.getAllDrinks()
},
// methods: {
// searchDrink(){
// if(!this.search){
// return this.drinks
// }else{
// return this.drinks.filter(drink =>
// drink.text.toLowerCase().includes(this.search.
// toLowerCase()))
// }
// }
// },
head(){
return {
title: 'Drinks App',
meta: [
{
hid: 'description',
name: 'description',
content: 'Best place to search a Drink'
}
]
}
}
}
</script>
you're not putting the image inside the src tag.
This can be done using the v-bind directive as
v-bind:src="..." or :src=".."
Example :
new Vue({
el: "#app",
data: () => ({
drinks: []
}),
mounted(){
axios.get('https://thecocktaildb.com/api/json/v1/1/search.php?s=')
.then((response) => {
this.drinks = response.data.drinks
})
}
})
img {
height: 200px;
width: 200px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.2.1/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<ul>
<li v-for="drink of drinks">
<img :src="drink.strDrinkThumb"/>
</li>
</ul>
</div>

Property or method "contact" is not defined on the instance but referenced during render Vuejs Laravel

I'm developing chat app on laravel using vue.js and i'm new to vue.js.
but i'm getting below mentioned error, please help me solve this
Error1 :
[Vue warn]: Property or method "contact" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property
Error2 :
[Vue warn]: Error in render: "TypeError: Cannot read property 'avatar' of undefined"
/ ChatApp.vue file/
<template>
<div class="chat-app">
<Conversation :contact="selectedContact" :messages="messages"/>
<ContactsList :contacts="contacts"/>
</div>
</template>
<script>
import Conversation from './Conversation.vue';
import ContactsList from './ContactsList.vue';
export default {
props: {
user: {
type: Object,
required: true
}
},
data(){
return {
selectedContact: null,
messages: [],
contacts: []
};
},
mounted(){
axios.get('/contacts')
.then((response) => {
this.contacts = response.data;
});
},
methods: {
startConversationWith(contact) {
axios.get(`/conversation/$(contact.id)`)
.then((response) => {
this.messages = response.data;
this.selectedContact = contact;
})
}
},
components: { ContactsList, Conversation }
};
</script>
/ ContactsList.vue file/
<template>
<div class="contacts-list">
<ul v-if="contacts.length > 0">
<li v-for:"(contact, index) In contacts" :key="contact.id"
#click="selectContact(index, contact)" :class="{ 'selected' : index ==
selected}">
<div class="avatar">
<img :src="contact.avatar" :alt="contact.name">
</div>
<div class="contact">
<p class="name">{{ contact.name }}</p>
<p class="email">{{ contact.email }}</p>
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
props: {
contacts: {
type: Array,
default: []
}
},
data() {
return {
selected: 0
};
},
methods: {
selectContact(index, contact) {
this.selected = index;
this.$emit('selected', contact);
}
}
};
</script>
/ conversation.vue /
<template>
<div class="conversation">
<h1>{{ contact ? contact.name : 'Select a contact' }}</h1>
<MessagesFeed :contact="contact" :messages="messages" />
<MessageComposer #send="sendMessage" />
</div>
</template>
<script>
import MessagesFeed from './MessagesFeed.vue';
import MessageComposer from './MessageComposer.vue';
export default {
props: {
contact: {
type: Object,
default: null
},
messages: {
type: Array,
default: []
}
},
methods:{
sendMessage(text) {
console.log(text);
}
},
components: {MessagesFeed, MessageComposer}
};
</script>
#Akash you can use it this way :
data() {
return {
contactsNotEmpty:false
}
},
// ...
mounted() {
axios.get('/contacts')
.then((response) => {
this.contacts = response.data;
this.contactsNotEmpty = true;
});
}
<ul v-if="contactsNotEmpty">
...
</ul>
also you may check this vuejs article about what is happening : https://vuejs.org/2016/02/06/common-gotchas/#Why-isn%E2%80%99t-the-DOM-updating
I guess you can do it like that:
<ul v-if="contacts.length > 0">
<li v-for="contact in contacts" :key="contact.id">
<div class="avatar">
<img :src="contact.avatar" :alt="contact.name">
</div>
<div class="contact">
<p class="name">{{ contact.name }}</p>
<p class="email">{{ contact.email }}</p>
</div>
</li>
</ul>
The first error is throwing in your Conversation.vue, you were passing data to a child component, you have to make sure it is defined as a prop in that component
export default{
props: ['contact']
};
and if it is defined, your prop type is not expecting a null value. I would change it to string and on rendering in my ChatApp.vue, I would set selectedContact to string.Empty||''
For the second error, the avatar key is missing from your response object. You have to check if it is present first before accessing it. Kindly refer to El Alami Anas's answer above.

Passing parameter from axios to laravel

In my application I'm working with comment, each product has its own comment.Comment in MySql contains column 'computer_comment' with foreign key(id).When I send 'computer_comment' from axios to laravel comments doesn't shown, I guess that 'computer_comment' is null or undefined.
I tried to send 1 or 2 instead 'computer_comment', in this case it works.
CommentComponent.vue
<template>
<div>
<div v-for="l in list">
<div v-if="l.computer_comment==current">
<div class="card" >
<div class="card body" >
<h4 style="text-decoration: underline">{{l.name}}</h4>
<p >{{l.body}}</p>
</div>
</div>
<br>
</div>
</div>
</div>
</template>
<script>
export default{
props: ['message'],
data(){
return{
list: [],
comment:{
id: '',
user_comment: '',
computer_comment: '',
name: '',
body: ''
},
current: this.message
}
},
created() {
this.show_comment();
},
methods: {
show_comment(){
axios.get('/current_comment').then(result => {
this.list = result.data;
console.log(result.data);
});
},
removeCom(id){
axios.post('/remove/'+id).then(result => {
this.show_comment();
}).catch(err => {
console.log(err);
});
}
},
}
</script>
info.blade.php
<div id="info">
<comment message="{{$computer->id}}"></comment>
.....
</div>

Laravel/Vue.js issues

I need help with my code please. I can't figure it out. I'm getting a "TypeError: this.$http is undefined" and it's driving me insane. I trace it to $http, but I have no idea how to fix it. I've just finished my Laravel course, but Vue.js is new to me. Here's is my code:
<template>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<textarea rows="3" class="form-control" v-model="content"></textarea>
<br>
<button class="btn btn-success pull-right" :disabled="not_working" #click="create_post()">
Create a post
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
mounted() {
},
data() {
return {
content: '',
not_working: true
}
},
methods: {
create_post() {
this.$http.post('/create/post', { content: this.content })
.then((resp) => {
this.content = ''
noty({
type: 'success',
layout: 'bottomLeft',
text: 'Your post has been published !'
})
console.log(resp)
})
}
},
watch: {
content() {
if(this.content.length > 0)
this.not_working = false
else
this.not_working = true
}
}
}
</script>
I think you have a error in submitting the post request.
you need to learn about axios.
Try this example.
axios.post('/create/post', this.content).then((response) => {
// do something
})
npm install axios --save
Add Axios to your Vue.js project, you have to import it:
<script>
import axios from 'axios';
export default {
data() {
return {
posts: [],
errors: []
}
},
created() {
let payload={
content:this.content
}
axios.post('/create/post',payload)
.then(response => {
this.posts = response.data
})
.catch(e => {
this.errors.push(e)
})
}
}
</script>

VueJs component: can't attribute axios reponse to a component variable

I am new to VueJs with Laravel. I have a component called Event in which a 'read more...' link shoud trigger a function that fetch the event infos from the data base and store the in a variable withing this same component.
Here is the code for my component:
<template>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h5>
Title: {{ event.event_name }}
<span class="pull-right">
#click=getEventInfo(event.id)>Read more ...</a></span>
</h5>
<h5>publised by: {{ event.user.name }}
<span class="pull-right" v-text="timeFromNow(event.created_at)"></span>
</h5>
</div>
<div class="panel-body">
<p>{{ event.event_detail }}</p>
</div>
<div class="panel-footer">
<p>
<span>Event for: {{ event.event_to_whom }}</span>
<span>When: {{ dayOfDate(event.event_when_date) }} {{ event.event_when_date }} at {{event.event_when_time}}</span>
</p>
</div>
</div>
</div>
</template>
<script>
import moment from 'moment';
export default {
name: 'app-event',
props:['event'],
data() {
return {
eventInfo: {}
}
},
methods: {
timeFromNow(tmp) {
return moment(tmp).fromNow();
},
dayOfDate(d) {
let dt = moment(d, "YYYY-MM-DD");
return dt.format('dddd');
},
//event.id
getEventInfo(id){
let url = 'events/' + id;
axios.get(url)
.then((response) => {
console.log(response.data); //returns data
this.eventInfo = response.data;
console.log(this.eventInfo);//returns data
});
console.log(this.eventInfo); //returns nothing
}
}
}
</script>
The problem is when I click the 'read more...' link axios returns a response that I can check using console.log(response.data). Hovever when I console.log(this.eventInfo) outside the axios closure, it returns and ampty object.
Am I doing something wrong? Sorry if the question is too basic.
Thanks
L.B
You are printing logging the property before it is assigned a value.
The code below should resolve any errors
import moment from 'moment';
export default {
name: 'app-event',
props:['event'],
data() {
return {
eventInfo: {}
}
},
methods: {
timeFromNow(tmp) {
return moment(tmp).fromNow();
},
dayOfDate(d) {
let dt = moment(d, "YYYY-MM-DD");
return dt.format('dddd');
},
//event.id
getEventInfo(id){
let url = 'events/' + id;
axios.get(url)
.then((response) => this.handleResponse(response.data));
},
handleResponse(eventInfo) {
console.log(eventInfo);
// do something with the eventInfo...
}
}
Your axios call is asynchrone so your outside console.log will be process before the return of the response.
Just keep your code in the .then((response) => { // here i proceed the read more }

Resources