Running node index.js does nothing
Asked Answered
T

3

6

I am trying to follow a simple tutorial on setting up algolia which one can find here.

https://www.algolia.com/doc/tutorials/indexing/3rd-party-service/firebase-algolia/

I literally copy the code word for word into an index.js file.

When I run node index.js in my terminal I get this

enter image description here

No success message. Nothing in algolia. Nothing. I have included my code is there anything that anyone noties that I may have done wrong. This has been frustrating me for hours.

const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');

// load values from the .env file in this directory into process.env
dotenv.load();

// configure firebase
firebase.initializeApp({
  databaseURL: process.env.FIREBASE_DATABASE_URL,
});
const database = firebase.database();

// configure algolia
const algolia = algoliasearch(
  process.env.ALGOLIA_APP_ID,
  process.env.ALGOLIA_API_KEY
);
const index = algolia.initIndex(process.env.ALGOLIA_INDEX_NAME);

  // Get all contacts from Firebase
  database.ref('/events').once('value', event => {
    console.log('index is', index);

    // Build an array of all records to push to Algolia
    const records = [];
    event.forEach(event => {

      // get the key and data from the snapshot
      const childKey = event.key;
      const childData = event.val();
      // We set the Algolia objectID as the Firebase .key
      childData.objectID = childKey;
      // Add object for indexing
      console.log('child data is', childData);

      records.push(childData);
    });

    // Add or update new objects
    index
      .saveObjects(records)
      .then(() => {
        console.log('records are', records);

        console.log('Events imported into Algolia');
      })
      .catch(error => {
        console.error('Error when importing events into Algolia', error);
        process.exit(1);
      });
  });

I have followed all steps in the tutorial. I even ran it again everytime I run node index.js nothing happens no matter what I do

Tolson answered 6/7, 2018 at 18:57 Comment(0)
T
1

The problem has to do with the database link. Check the value in the .env file of FIREBASE_DATABASE_URL , it should be something like: https://test123-dsacb.firebaseio.com

Turbofan answered 6/7, 2018 at 19:44 Comment(5)
yeah so i logged the url and thats what it looks likeTolson
but when i go back to check algolia i see no records nowTolson
do you have any data in the events collection in firebase?Turbofan
are you familar with algoliaTolson
TBH, first time heard about it. Also first time testing smth with firebase :DTurbofan
S
13

Happened to me, the issue was I haven't actually saved the index.js file. Once I saved it, it worked.

Sexed answered 13/8, 2021 at 10:2 Comment(1)
believe it or not, it happened to me too xD!!!!Nob
T
1

The problem has to do with the database link. Check the value in the .env file of FIREBASE_DATABASE_URL , it should be something like: https://test123-dsacb.firebaseio.com

Turbofan answered 6/7, 2018 at 19:44 Comment(5)
yeah so i logged the url and thats what it looks likeTolson
but when i go back to check algolia i see no records nowTolson
do you have any data in the events collection in firebase?Turbofan
are you familar with algoliaTolson
TBH, first time heard about it. Also first time testing smth with firebase :DTurbofan
W
0

Look it again:

https://www.algolia.com/doc/tutorials/indexing/3rd-party-service/firebase-algolia/

It says first you need to install these modules:

npm install dotenv --save
npm install algoliasearch --save
npm install firebase --save

and then set your environment variable:

    export ALGOLIA_APP_ID=<algolia-app-id>
    export ALGOLIA_API_KEY=<algolia-api-key>
    export ALGOLIA_INDEX_NAME='contacts'
    export FIREBASE_DATABASE_URL=https://<my-firebase-database>.firebaseio.com

Try these things, if you still gets nothing then I think you have to emit or call '/events' event from somewhere to console the console.log('index is', index); part.

Whitewood answered 6/7, 2018 at 19:10 Comment(11)
it told me to setup my environment in a file called '.env' i did thatTolson
still does nothingTolson
trust me i atlast followed all these steps before postingTolson
Then check console is inside the /events event or api or connection, so as per my understanding we have to hit the event to check the logs = onsole.log('index is', index);.Whitewood
the database call never executesTolson
i have a statement console.log('before database'); in addition to console.log('entered database call'); the second one never occursTolson
Have you reached inside /events ?Whitewood
no I have not reached inside the database.ref('/events') callbackTolson
Ok just set env variable through export to make sure your code is correct.Whitewood
example? pleaseTolson
like export YOUR_ENV_VARIABLE=<env_variable_value>Whitewood

© 2022 - 2024 — McMap. All rights reserved.