travis-r6s / gridsome-plugin-flexsearch

Add lightning fast search to Gridsome with FlexSearch
24 stars 7 forks source link

Strange Issue trying to return url from searchResults array. #38

Closed darrylmorley closed 4 years ago

darrylmorley commented 4 years ago

Hi,

I have a really weird issue. I have the below available in my searchResults array.

images:"[{"Number":1,"FullPath":"https://images.guntrader.uk/GunImages/19/1911/19112/191128124520003/191128124520003-1.jpg","ThumbPath":"https://images.guntrader.uk/GunImages/Thumbnails/191128124520003-1-120x120.jpg"},{"Number":2,"FullPath":"https://images.guntrader.uk/GunImages/19/1911/19112/191128124520003/191128124520003-2.jpg","ThumbPath":"https://images.guntrader.uk/GunImages/Thumbnails/191128124520003-2-120x120.jpg"}]"

When I try to select an image to display in my search results, it selects the characters rather than the url. I have <g-image :src="`${result.images[0].FullPath}`" :alt="`${result.title}`" /> to select my image, this returns undefined. If I change this a little to <g-image :src="`${result.images[0]}`" :alt="`${result.title}`" /> I receive "[".

I am getting characters instead of the object?

travis-r6s commented 4 years ago

Is that array the exact result you get from your search results? As you can see, it is actually a string - note the " either side of the array. So when you do [0], it selects the first character.

I'll have a look into this, as to why it isn't parsing the array properly. Could you paste your flexsearch config here please?

darrylmorley commented 4 years ago

Ahh yes I see, so it is. My config is:

{
      use: "gridsome-plugin-flexsearch",
      options: {
        collections: [
          {
            typeName: "Guns",
            indexName: "Guns",
            fields: [
              "title",
              "slug",
              "images",
              "price",
              "mechanism",
              "id",
              "variant",
              "condition",
              "calibre"
            ]
          }
        ],
        searchFields: ["title", "mechanism", "calibre"]
      }
    }
Screenshot 2020-05-11 at 19 23 02

When I log out options on build, the array is not a string.

images: [
    {
      Number: 1,
      FullPath: 'https://images.guntrader.uk/GunImages/20/2003/20030/200303114752001/200303114752001-1.jpg',
      ThumbPath: 'https://images.guntrader.uk/GunImages/Thumbnails/200303114752001-1-120x120.jpg'
    },
travis-r6s commented 4 years ago

I have fixed this in a new version (which includes a number of updates), but am awaiting for a PR in Gridsome. For now you could just do JSON.parse(<result>.images) which will return the array.

darrylmorley commented 4 years ago

Thanks for your help, I'm still seeing the same issue here though (using the updated plugin).

travis-r6s commented 4 years ago

Yes, sorry - I haven't actually released the update yet...

darrylmorley commented 4 years ago

Ha, I ran an update which upgraded your plugin I figured you had released it. Sorry.