0

I use Vue framework and i have a .vue file in which i want to get the current Country on created(). My code is below:

 <template>
    <div id="show-blogs">
        <h1>All Blog Articles</h1>
        <input type="text" v-model="search" placeholder="search blogs" />
        <!-- INPUT -->
        <input type="text" v-model="message" required id="selectedCountry"/>
        <!-- BUTTON ADD BLOG -->
        <button v-on:click="selectCountry">Select country</button>

        <div v-for="blog in filteredBlogs" class="single-blog">
            <!-- router-link = href -->
            <router-link v-bind:to="'/blog/' + blog.id">
                <h2>{{ blog.country }}</h2>
            </router-link>
            <p>{{ blog.city }}</p>
            <p>{{ blog.category }}</p>
            <p>{{ blog.phoneNumber }}</p>

        </div>
    </div>
</template>

<script>        
    var currentCountry = getCurrentCountry();        

    import searchMixin from '../mixins/searchMixin';

    export default {        

        data() {


            return {
                blogs: [],
                search: '',

            }
        },    


        methods: {
            selectCountry: function() {
                selectedCountry = document.getElementById("selectedCountry").value;
                this.$http.get('https://aggelies-7fde2.firebaseio.com/posts/' + selectedCountry + '.json').then(function(data) {

                    return data.json()
                }).then(function(data) {
                    var blogsArray = [];
                    for (let key in data) {
                        data[key].id = key;
                        blogsArray.push(data[key]);
                    }
                    this.blogs = blogsArray;

                });

            },


            getCurrentCountry: function() {
                var currCountry;

                var requestUrl = "http://ip-api.com/json";

                $.ajax({
                    url: requestUrl,
                    type: 'GET',
                    async: false,
                    success: function(json) {

                        currCountry = json.country.toUpperCase();

                    },
                    error: function(err) {

                    }
                });

                alert(currCountry);
                return currCountry;

            }

        },


        created() {

            this.$http.get('https://aggelies-7fde2.firebaseio.com/posts/' + currentCountry + '.json').then(function(data) {

                return data.json()
            }).then(function(data) {
                var blogsArray = [];
                for (let key in data) {
                    data[key].id = key;
                    blogsArray.push(data[key]);
                }
                this.blogs = blogsArray;
                //console.log(this.blogs);
            });
        },


        mixins: [searchMixin]
    }
</script>

<style scoped>
    .....
</style>

the ajax code is working fine and i get current country. But when i call in the beginning of the script getCurrentCountry(), the function does not work and i dont get the return value so as to assign it to currentCountry. Any suggestions what is wrong?

ktsakiris
  • 59
  • 1
  • 10
  • 1
    Never ever use `async: false` it is a terrible practice and is deprecated . Look at the warnings in browser console – charlietfl Oct 14 '17 at 14:12
  • Why is `currentCountry` not part of the vue component data? `getCurrentCountry` is not defined at the first line of the script because it is defined in the exported object. – Wouter Florijn Oct 14 '17 at 14:13
  • so you mean i must define getCurrentCountry() somewhere else? – ktsakiris Oct 14 '17 at 14:20
  • No, I'm saying you should make `currentCountry` part of the data and call `getCurrentCountry` from inside the vue component (probably in the `created` function). I can't tell you exactly how because your code is a bit confusing to me. Please provide a [minimal example](https://stackoverflow.com/help/mcve). – Wouter Florijn Oct 14 '17 at 14:35

0 Answers0