Running node index.js does nothing

北战南征 提交于 2020-12-26 08:08:28

问题


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

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


回答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




回答2:


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.



来源:https://stackoverflow.com/questions/51216276/running-node-index-js-does-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!