Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Twovo opened this issue Dec 4, 2024 · 3 comments
Closed

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

Twovo opened this issue Dec 4, 2024 · 3 comments
Labels

Comments

@Twovo
Copy link

Twovo commented Dec 4, 2024

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

@Twovo Twovo added the bug label Dec 4, 2024
@Haroenv
Copy link
Contributor

Haroenv commented Dec 4, 2024

Are the environment variables actually set? often there's a typo or a slightly different version of the environment variable spelled

@Twovo
Copy link
Author

Twovo commented Dec 4, 2024

UPDATE

I got this working by hardcoding all the API creds instead of using a .env file. For whatever reason it wasn't reading it.

Thanks @Haroenv for the inspiration to check!

@Haroenv Haroenv closed this as completed Dec 4, 2024
@Haroenv
Copy link
Contributor

Haroenv commented Dec 4, 2024

Glad that helped! Not sure why the variables weren't set, but I'm guessing that's a firebase configuration setup issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants