Skip to content

[bug]: Invalid value "undefined" for header "x-algolia-api-key" #1572

Closed
@Twovo

Description

@Twovo

Description

I receive the following error when trying to sync Firestore to Algolia:

Error syncing to Algolia: TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value "undefined" for header "x-algolia-api-key"
at ClientRequest.setHeader (node:_http_outgoing:662:3)
at new ClientRequest (node:_http_client:289:14)
at Object.request (node:https:379:10)
at C:\Webdev\Bp\node_modules@algolia\requester-node-http\dist\requester-node-http.cjs.js:33:72
at new Promise ()
at Object.send (C:\Webdev\Bp\node_modules@algolia\requester-node-http\dist\requester-node-http.cjs.js:18:20)
at retry (C:\Webdev\Bp\node_modules@algolia\transporter\dist\transporter.cjs.js:222:38)
at C:\Webdev\Bp\node_modules@algolia\transporter\dist\transporter.cjs.js:235:16
at syncFirestoreToAlgolia (C:\Webdev\Bp\scripts\syncFirestoreToAlgolia.ts:17:9) {
code: 'ERR_HTTP_INVALID_HEADER_VALUE'
}

Code using for the sync:

firebase.ts

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {
    apiKey: '***',
    authDomain: '***',
    projectId: '***',
    storageBucket: '***',
    messagingSenderId: '***',
    appId: '***',
};

const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

algolia.ts

import algoliasearch from 'algoliasearch';

const client = algoliasearch(
    process.env.ALGOLIA_APP_ID!,
    process.env.ALGOLIA_API_KEY!
);
export const index = client.initIndex(process.env.ALGOLIA_INDEX_NAME!);

syncFireStoreToAlgolia.ts (this is what is run for the sync)

import { collection, getDocs } from 'firebase/firestore';
import { db } from './firebase';
import { index } from './algolia';

async function syncFirestoreToAlgolia() {
    const snapshot = await getDocs(collection(db, 'mode/production/bookings'));
    const records = snapshot.docs.map((doc) => ({
        objectID: doc.id,
        ...doc.data(),
    }));
    
    // Here i'm logging the records to check they come back successfully - they do
    console.log(
        '🚀 ~ file: syncFirestoreToAlgolia.ts:8 ~ records ~ records:',
        records
    );

    try {
        await index.saveObjects(records);
        console.log('Firestore data synced to Algolia successfully!');
    } catch (error) {
        console.error('Error syncing to Algolia:', error);
    }
}

syncFirestoreToAlgolia();

I have checked that firebase settings are correct, they are:

> firebase functions:config:get
{
  "algolia": {
    "app_id": "***",
    "api_key": "***",
    "search_key": "***",
    "app": "***"
  }
}

How can I fix this x-algolia-api-key error?

Invalid value "undefined" for header "x-algolia-api-key"

Client

All

Version

4.24.0 (tried 5 but that gave errors that initIndex not a func)

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions