首页 > 解决方案 > 使用 firebase 设置 algolia 时出错

问题描述

您好,我正在尝试设置 algolia 以在此处使用我的 firebase 数据库,但遇到了一些麻烦。我一步一步地遵循了这个指南。

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

但是,当我尝试运行它时,我仍然收到此错误

'Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail and then I comment out this line.'

我不确定我的代码有什么问题我遵循了所有内容,但我仍然遇到问题。我已经包含了我的 index.js 中的一些代码,看看是否有人能看到我做错了什么。

const functions = require('firebase-functions');
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

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


 // Initialize Firebase
 var config = {
    apiKey: "AIzaSyA25OPWS1ign1vKkg7_MFo5SwtqSBezpoo",
    authDomain: "eventful-3d558.firebaseapp.com",
    databaseURL: process.env.FIREBASE_DATABASE_URL,
    projectId: "eventful-3d558",
    storageBucket: "eventful-3d558.appspot.com",
    messagingSenderId: "323022069474"
  };

  firebase.initializeApp(config);

  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 => {

    // 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
      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);
      });
  });

标签: node.jsfirebasealgolia

解决方案


推荐阅读