Searching through Flickr API by tags


#1

I’m using Vue to create a simple Flickr app and want to add a search bar so users can search for photos containing tags with their search term.

What i’ve done so far produces some results, but I noticed the photos don’t always include my search term as tags. Also, I am getting the error ‘Invalid handler for event “change”: got undefined’ everytime i type in the search box but i’m unsure how to resolve this.

<template>
  <b-container>
    <b-row>
      <b-col md="12">
        <b-input-group size="lg" prepend="Search" class="flickr-search">
          <b-form-input v-model="search" @change="filteredPhotos"></b-form-input>
        </b-input-group>
      </b-col>
    </b-row>
    <b-row>
      <b-card-group columns>
        <b-col v-for="photo in Photos" class="item" md="12">
          <b-card :title="photo.title"
                  :img-src="photo.media.m"
                  img-alt="Image"
                  img-top
                  img-fluid
                  tag="article"
                  style="max-width: 20rem;"
                  class="mb-2">
            <span class="item-date">31 May 2017</span>
            <hr/>
            <p>By <a :href="'https://www.flickr.com/photos/' + photo.author_id" :title="formatAuthor(photo.author)" target="_blank">{{ formatAuthor(photo.author) }}</a></p>
            <ul class="tags">
              <li v-for="tag in splitTags(photo.tags)" class="item-tag">
                <a :href="'https://www.flickr.com/photos/tags/' + tag" target="_blank" class="item-taglink">{{ tag }}</a>
              </li>
            </ul>
          </b-card>
        </b-col>
      </b-card-group>
    </b-row>
  </b-container>
</template>

<script>
    import jsonp from "jsonp";

export default {
    name: 'PhotoFeed',
    data: function () {
        return {
            Photos: [],
            apiURL: "https://api.flickr.com/services/feeds/photos_public.gne?format=json",
            search: ''
        }
    },
    mounted(){
        this.getFlickrFeed();
    },
    methods: {
        getFlickrFeed(){
            let jsonp = require('jsonp');
            let self = this;

            jsonp(this.apiURL, {name: 'jsonFlickrFeed'}, (err, data) => {
                if (err) {
                    console.log(err.message);
                }
                else {
                    self.Photos = data.items;
                }
            })
        },
        formatAuthor(authorString){
            if (authorString) return authorString.split("\"")[1];
            return "Author";
        },
        splitTags(tagsString) {
            if (tagsString) return tagsString.split(" ");
        }
    },
    computed: {
        filteredPhotos(){
            let self = this;
            let apiURL = "https://api.flickr.com/services/feeds/photos_public.gne?tags=" + self.search + "&format=json";
            let jsonp = require('jsonp');

            jsonp(apiURL, {name: 'jsonFlickrFeed'}, (err, data) => {
                if (err) {
                    console.log(err.message);
                }
                else {
                    self.Photos = data.items;
                }
            })
        }
    }
}
</script>

#2

The error is because you are using v-model and change event in one element. It’s either one or another.

Do it with a watcher:

<b-form-input v-model="search" />
watch: {
  search(newVal, oldVal) {
    // do your fetch here
  }
}

Also I strongly suggest debouncing search so it is not triggered on every keystroke.