I have a problem when I try to push my MySQL data to Algolia

I am trying to push the data of my data base in Mysql to Algolia. This is the code.

const algoliasearch = require("algoliasearch");
const { ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY} = process.env;
const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY);
const index = client.initIndex("demo_ecommerce");
const _ = require('lodash');

var mysql = require('mysql');

var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : 'password',
    database : 'ecommerce'
})

connection.connect();

connection.query("SELECT * FROM productos", (err, res) => {
  if(err){
    console.log(err)
  } else {
    const chunks = _.chunk(res, 1000) 
    chunks.forEach(chunk => index.saveObjects(chunk,{'autoGenerateObjectIDIfNotExist': true}).then(console.log(chunk)).catch(err => {console.log(err)}))
  }
});

My .env is this

ALGOLIA_APPLICATION_ID=Y4VCCCZ5L1

ALGOLIA_ADMIN_API_KEY=fadfa3960c72b2db95c5bb0436780fde

The error that I recieve is the next

    at ClientRequest.setHeader (_http_outgoing.js:521:3)
    at new ClientRequest (_http_client.js:241:14)
    at Object.request (https.js:314:10)
    at C:\Users\giuliana.olmos\Desktop\Giuli\AlgoliaBackend\node_modules\@algolia\requester-node-http\dist\requester-node-http.cjs.js:27:72
    at new Promise (<anonymous>)
    at Object.send (C:\Users\giuliana.olmos\Desktop\Giuli\AlgoliaBackend\node_modules\@algolia\requester-node-http\dist\requester-node-http.cjs.js:16:20)
    at retry (C:\Users\giuliana.olmos\Desktop\Giuli\AlgoliaBackend\node_modules\@algolia\transporter\dist\transporter.cjs.js:222:38)
    at C:\Users\giuliana.olmos\Desktop\Giuli\AlgoliaBackend\node_modules\@algolia\transporter\dist\transporter.cjs.js:235:16     
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'ERR_HTTP_INVALID_HEADER_VALUE'
} ```

Hi @giuliana.olmos,

Could you provide a bit more information about your setup? Which version of algoliasearch are you using? I tested the logic on my side (without the DB) and I managed to index the products.

Note that the program doesn’t wait for request made by the client. You should update slightly the program to make sure the requests actually happen without error. Here is the code that I’ve run:

connection.query('SELECT * FROM productos', async (err, res) => {
  if (err) {
    throw err;
  }

  const chunks = _.chunk(res, 1000);

  const responses = await Promise.all(
    chunks.map((chunk) =>
      index
        .saveObjects(chunk, { autoGenerateObjectIDIfNotExist: true })
        .then((task) => ({ task, chunk }))
    )
  ).catch((err) => {
    console.log(err);
  });

  console.log(responses);
});

Hope that helps!

Hi!
Thank u for the code. I used it but the error is still the same.
I am using this version of algoliasearch
“algoliasearch”: “^4.5.1”,

If you log out the environment variables before using them, is it possible they are undefined? I think this error can happen if a header is undefined.

const { ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY} = process.env;
console.log({ ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY });
const client = algoliasearch(ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY);