copyIndex then searching items from new index gives an error

When I try to do a search on a newly copied index in the callback to copyIndex I get an error saying the “new index does not exist.” However, if I do addObjects on the newly copied index, everything works ok. I was under the impression that the callback is called when the new index is ready? In the meantime, I’m working around this by adding a 3 second timeout before searching, but it seems like a hacky solution.

client.copyIndex(oldIndex, newIndex, function() {
    const index = client.initIndex(newIndex);, function(err) {
        /* we get an error that index `newIndex` does not exist */

    index.addObjects(/* ...*/, function() {
        /* no error here */

Any indexing operation on Algolia is asynchronous.

Getting the response from Algolia it:

  • is an acknowledgment from algolia that the job has been received
  • it does not mean that it has yet been executed/applied on the cluster.

Adding objects on an non-existing index will create the index that’s why you addObject works fine.
Searching on an non-existing index returns an error that’s why the call is failing.

To summarise in your copyIndex callback there is no guaranty that the index exist.

Ah ok, that makes more sense now. Is there an api we can use to know when the new indexing operation is complete? Or is the only way to poll the search api until it succeeds?