Method not correctly executed on initial load
I have a method to build the URL for an club logo image and if that image doesn't exist, it builds an URL for the league logo image (it is not the perfect method yet, it needs to be refactored, but it works for now):
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
When the page is loaded first (it loads the data with axios from an API in mounted), it shows a broken image. This is because src is filled with the club name image, which doesn't exist. When I then click on a link on the page and then return to this page with a click on a link, it correctly shows the league image.
This is the console output when the initial page is loaded (it loads the specific image which doesn't exist):
FIRST /images/logos/maidenhead-united.png
THIRD /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
And when the page is loaded from clicking a link (correct image shown):
FIRST /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
THIRD /images/leagues/the-conference.png
So why is the default image not loaded on initial page load, but it is loaded when clicking a link? Could it be the data has not finished loading, but the method is already executed? This is all code from this component:
<template>
<div class="visits row">
<div class="col-sm-3 visit" v-for="visit in visits" :key="visit.nr">
<div class="card">
<div class="card-header">{{ visit.nr }}</div>
<div class="card-body">
<router-link :to="{ name : 'VisitDetails', params: {id: visit.id}}">
<h5 class="card-title">{{ visit.home }}</h5>
</router-link>
<div class="logo">
<img :src="getLogoUrl(visit.home, visit.league)" class="card-img-center img-responsive center-block">
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'home',
data () {
return {
feedback: null,
visits: ,
errors:
}
},
methods: {
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
},
mounted() {
axios
.get(this.$baseURL + "/footmarks")
.then(response => {
console.log(response.data)
this.visits = response.data
})
.catch(e => {
console.log(this.errors)
})
}
}
</script>
<style scoped>
</style>
javascript vue.js callback
add a comment |
I have a method to build the URL for an club logo image and if that image doesn't exist, it builds an URL for the league logo image (it is not the perfect method yet, it needs to be refactored, but it works for now):
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
When the page is loaded first (it loads the data with axios from an API in mounted), it shows a broken image. This is because src is filled with the club name image, which doesn't exist. When I then click on a link on the page and then return to this page with a click on a link, it correctly shows the league image.
This is the console output when the initial page is loaded (it loads the specific image which doesn't exist):
FIRST /images/logos/maidenhead-united.png
THIRD /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
And when the page is loaded from clicking a link (correct image shown):
FIRST /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
THIRD /images/leagues/the-conference.png
So why is the default image not loaded on initial page load, but it is loaded when clicking a link? Could it be the data has not finished loading, but the method is already executed? This is all code from this component:
<template>
<div class="visits row">
<div class="col-sm-3 visit" v-for="visit in visits" :key="visit.nr">
<div class="card">
<div class="card-header">{{ visit.nr }}</div>
<div class="card-body">
<router-link :to="{ name : 'VisitDetails', params: {id: visit.id}}">
<h5 class="card-title">{{ visit.home }}</h5>
</router-link>
<div class="logo">
<img :src="getLogoUrl(visit.home, visit.league)" class="card-img-center img-responsive center-block">
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'home',
data () {
return {
feedback: null,
visits: ,
errors:
}
},
methods: {
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
},
mounted() {
axios
.get(this.$baseURL + "/footmarks")
.then(response => {
console.log(response.data)
this.visits = response.data
})
.catch(e => {
console.log(this.errors)
})
}
}
</script>
<style scoped>
</style>
javascript vue.js callback
add a comment |
I have a method to build the URL for an club logo image and if that image doesn't exist, it builds an URL for the league logo image (it is not the perfect method yet, it needs to be refactored, but it works for now):
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
When the page is loaded first (it loads the data with axios from an API in mounted), it shows a broken image. This is because src is filled with the club name image, which doesn't exist. When I then click on a link on the page and then return to this page with a click on a link, it correctly shows the league image.
This is the console output when the initial page is loaded (it loads the specific image which doesn't exist):
FIRST /images/logos/maidenhead-united.png
THIRD /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
And when the page is loaded from clicking a link (correct image shown):
FIRST /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
THIRD /images/leagues/the-conference.png
So why is the default image not loaded on initial page load, but it is loaded when clicking a link? Could it be the data has not finished loading, but the method is already executed? This is all code from this component:
<template>
<div class="visits row">
<div class="col-sm-3 visit" v-for="visit in visits" :key="visit.nr">
<div class="card">
<div class="card-header">{{ visit.nr }}</div>
<div class="card-body">
<router-link :to="{ name : 'VisitDetails', params: {id: visit.id}}">
<h5 class="card-title">{{ visit.home }}</h5>
</router-link>
<div class="logo">
<img :src="getLogoUrl(visit.home, visit.league)" class="card-img-center img-responsive center-block">
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'home',
data () {
return {
feedback: null,
visits: ,
errors:
}
},
methods: {
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
},
mounted() {
axios
.get(this.$baseURL + "/footmarks")
.then(response => {
console.log(response.data)
this.visits = response.data
})
.catch(e => {
console.log(this.errors)
})
}
}
</script>
<style scoped>
</style>
javascript vue.js callback
I have a method to build the URL for an club logo image and if that image doesn't exist, it builds an URL for the league logo image (it is not the perfect method yet, it needs to be refactored, but it works for now):
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
When the page is loaded first (it loads the data with axios from an API in mounted), it shows a broken image. This is because src is filled with the club name image, which doesn't exist. When I then click on a link on the page and then return to this page with a click on a link, it correctly shows the league image.
This is the console output when the initial page is loaded (it loads the specific image which doesn't exist):
FIRST /images/logos/maidenhead-united.png
THIRD /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
And when the page is loaded from clicking a link (correct image shown):
FIRST /images/logos/maidenhead-united.png
SECOND /images/leagues/the-conference.png
THIRD /images/leagues/the-conference.png
So why is the default image not loaded on initial page load, but it is loaded when clicking a link? Could it be the data has not finished loading, but the method is already executed? This is all code from this component:
<template>
<div class="visits row">
<div class="col-sm-3 visit" v-for="visit in visits" :key="visit.nr">
<div class="card">
<div class="card-header">{{ visit.nr }}</div>
<div class="card-body">
<router-link :to="{ name : 'VisitDetails', params: {id: visit.id}}">
<h5 class="card-title">{{ visit.home }}</h5>
</router-link>
<div class="logo">
<img :src="getLogoUrl(visit.home, visit.league)" class="card-img-center img-responsive center-block">
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'home',
data () {
return {
feedback: null,
visits: ,
errors:
}
},
methods: {
getLogoUrl: function(club, league) {
let imageExists = require('image-exists');
// First check if club logo exists
let src = "/images/logos/" + club + ".png";
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
}
});
console.log("THIRD "+src);
return src;
}
},
mounted() {
axios
.get(this.$baseURL + "/footmarks")
.then(response => {
console.log(response.data)
this.visits = response.data
})
.catch(e => {
console.log(this.errors)
})
}
}
</script>
<style scoped>
</style>
javascript vue.js callback
javascript vue.js callback
edited Dec 27 at 13:29
BlackICE
6,90024075
6,90024075
asked Dec 27 at 13:13
John
2,31483675
2,31483675
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It would appear that imageExists is using async code and returns the value in the callback after it's done asynchronously checking if the image exists the first time through, occurring after the THIRD in your code, but the second time it's loaded, it already has the value, and thus SECOND is called immediately and occurs before THIRD.
One thing you could do is set the default value within the callback:
let src = '/images/some-loading-image.png';
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
} else {
src = "/images/logos/" + club + ".png";
}
});
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945694%2fmethod-not-correctly-executed-on-initial-load%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
It would appear that imageExists is using async code and returns the value in the callback after it's done asynchronously checking if the image exists the first time through, occurring after the THIRD in your code, but the second time it's loaded, it already has the value, and thus SECOND is called immediately and occurs before THIRD.
One thing you could do is set the default value within the callback:
let src = '/images/some-loading-image.png';
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
} else {
src = "/images/logos/" + club + ".png";
}
});
add a comment |
It would appear that imageExists is using async code and returns the value in the callback after it's done asynchronously checking if the image exists the first time through, occurring after the THIRD in your code, but the second time it's loaded, it already has the value, and thus SECOND is called immediately and occurs before THIRD.
One thing you could do is set the default value within the callback:
let src = '/images/some-loading-image.png';
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
} else {
src = "/images/logos/" + club + ".png";
}
});
add a comment |
It would appear that imageExists is using async code and returns the value in the callback after it's done asynchronously checking if the image exists the first time through, occurring after the THIRD in your code, but the second time it's loaded, it already has the value, and thus SECOND is called immediately and occurs before THIRD.
One thing you could do is set the default value within the callback:
let src = '/images/some-loading-image.png';
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
} else {
src = "/images/logos/" + club + ".png";
}
});
It would appear that imageExists is using async code and returns the value in the callback after it's done asynchronously checking if the image exists the first time through, occurring after the THIRD in your code, but the second time it's loaded, it already has the value, and thus SECOND is called immediately and occurs before THIRD.
One thing you could do is set the default value within the callback:
let src = '/images/some-loading-image.png';
console.log("FIRST "+src);
imageExists(src, function(exists) {
// Then get league logo
if (!exists) {
src = "/images/leagues/" + league + ".png";
console.log("SECOND "+src);
} else {
src = "/images/logos/" + club + ".png";
}
});
answered Dec 27 at 13:23
BlackICE
6,90024075
6,90024075
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945694%2fmethod-not-correctly-executed-on-initial-load%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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