algolia / vue-instantsearch

👀 Algolia components for building search UIs with Vue.js

Home Page:https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/vue

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"indexName is not valid" error in Algolia after Searchable attributes update

tom43983 opened this issue · comments

I have implemented Algolia Instant Search in a Vue3 app via the Firebase/Algolia extension.

Everything was working fine, I could do searches from my frontend and get the expected results.

Then I changed the Searchable attributes on the Algolia dashboard and suddenly I get only empty results (empty data array in Hits) client side.

When checking the chrome dev tools, network tab, the index name of the request is correct, aswell as the search query. But I also get this message:

{"message":"indexName is not valid","status":400}

When I do the search in the Algolia dashboard, everything works fine, I get the expected results.

The only thing I changed are the searchable attributes on the Algolia dashboard. I have tried with different attributes, aswell as leaving it empty (which should make Algolia search on all fields). The Hits data client side is always empty, as shown in the console.log below.

How can the indexName not be valid if it is correct client side and the search works fine on the Algolia dashboard??

Thanks for any help!!

<template>

  <input type="text" v-model="$store.state.searchInputText" />

  <ais-instant-search :search-client="searchClient" index-name="profiles">
    <ais-search-box
      class="d-none mb-4"
      v-model="$store.state.searchInputText"
      :class-names="{
        'ais-SearchBox-input':
          'search-input form-control form-control-flush ps-10',
      }"
    />

    <ais-hits
      v-if="transformItems.length > 0"
      :transform-items="transformItems"
    >
      <template v-slot:item="{ item }">
        <div class="container">
          <!-- Begin result (profile row) -->
          {{ item.firstname }} {{ item.lastname }}
          <!-- End result (profile row) -->
        </div>
      </template>
    </ais-hits>

    <div class="d-flex justify-content-center flex-nowrap mt-6">
      <button
        class="btn btn-sm btn-primary"
        v-if="$store.state.searchResults.length > 3"
        @click="sendResultsToStore()"
      >
        See all results
      </button>
    </div>

    <div class="mt-4"></div>
  </ais-instant-search>
</template>

<script>
import algoliasearch from "algoliasearch/lite";
import "instantsearch.css/themes/satellite-min.css";
import { goToDetailedProfileExternal } from "../js/navigation";
import store from "../store/index";

export default {
  data() {
    return {
      searchClient: algoliasearch(
        "XXXXXXXXXXX",
        this.$store.state.userData.algoliasearchkey
      ),
      inputtext: "",
    };
  },
  methods: {
    transformItems(items) {
      store.state.searchResults = [];

      for (let i = 0; i < items.length; i++) {
        let profile = items[i];
        profile.id = items[i].objectID;
        store.state.searchResults.push(profile);
      }

      ////////////here is the problem, this is always empty!
      console.log(store.state.searchResults);

      return items.slice(0, 3);
    },
  },
};
</script>


Is the indexName everywhere profiles? does algoliasearchkey have access to that index as well? Does it work if you use a different api key?

Thanks, so "profiles" is the index name everywhere. algoliasearchkey is only the API key value. It works when I use a dashboard generated API key. Otherwise the way I generate the key for each user is through a cloud function in Firebase. This is how I do it in my cloud functions:

//generate a key for each new signed-up user, then add it to the user document
const algoliasearch = require('algoliasearch');
const client = algoliasearch('xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
const index = client.initIndex('profiles');

exports.generateAndAddAlgoliaApiKeyToUser = functions.firestore
  .document('users/{user}')
  .onCreate((snap, context) => {
    console.error("On Create triggered")



    const userid = snap.id

    console.log('the id of the new user id : ' + userid)

    const publicKey = client.generateSecuredApiKey(
      'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // A search key that you keep private
      {
        filters: `owninguser:${userid}`
      }
    );

    console.log("this is the new key: " + publicKey)



    db.doc('users/' + snap.id).update({
      algoliasearchkey: publicKey,
    });

I realise I don't use the index variable in the function. Is this what causes the key not to work?

https://www.algolia.com/doc/api-reference/api-methods/generate-secured-api-key/

This is the guide I used to create the key generation function. I don't see any obligation to specify an index. I just restrict the search for each user with a filter.

No, this all should work. As it's a question unrelated to InstantSearch as far as I can tell, I suggest you contact support@algolia.com describing your issue. I don't know what the cause could be, sorry!