node.js - 使用 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);
});
});
解决方案
推荐阅读
- python - 更改熊猫列中的数字字符串
- sql - 为注册编写 SQL 查询
- jquery - 一般错误:1364 字段没有默认值
- html - 固定侧边栏内的狮身人面像雪花石膏主题滚动
- javascript - 在客户端转义编码的 base 64 字符串,以便它可以在服务器端正常工作和读取
- reactjs - 什么是最好的反应钩子,频率代码或性能
- asp.net - 在 plesk 上发布 asp.net 网站时出错
- python - numpy 数组中非唯一行的快速组合,映射到列(即快速数据透视表问题,没有 Pandas)
- linux - 启动后自动运行烧瓶应用程序无法正常工作
- python-3.x - 为什么使用shututil在循环内移动文件时找不到文件错误?