Algolia Livesearch将Firestore数据编入Algolia

时间:2019-09-05 15:10:50

标签: google-cloud-firestore algolia

我正在尝试在Web项目中使用Algolia for Firestore。。我正在遵循下一页上的指南。第一步,他们要求您将Firestore与Algolia同步。

https://medium.com/@soares.rfarias/how-to-index-firestore-data-into-algolia-using-cloud-functions-9ed5b56baa57

我在Node.js中设置了所有内容,然后运行了代码。与Firestore的连接很好,我在控制台中看到数据库中的所有项目。与Algolia的连接尚不起作用。

我在Node.js控制台中收到以下错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出一个异步函数而引起的,或者是由于拒绝了一个不存在的承诺而导致的 .catch()。

这部分代码未运行。 有谁可以建议我如何解决这个问题?

    // Add or update new objects
    index.saveObjects(records).then(() => {
        console.log('Documents imported into Algolia');
        process.exit(0);
    })

我在FireStore中的安全规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

// API key needs to have write access 
// credentials off the app 
ALGOLIA_APP_ID="xxxx"
ALGOLIA_API_KEY="xxxx"
ALGOLIA_INDEX_NAME='xxxx'
FIREBASE_DATABASE_URL= "xxxx";
FIREBASE_PROJECT_ID= "xxxx";

// YOU GET YOUR DATA IN CONSOLE BUT YOUR ARE NOT SAVING THEM TO ALGOLIA
// CONNECTION WITH FIRESTORE IS FINE, CONNECTION WITH ALGOLIA NOT 

// APP DEPENDENCIES 
const algoliasearch = require('algoliasearch');
const dotenv = require('dotenv');
const firebase = require('firebase');
const firestore = require('firebase/firestore');

// INITIALIZE FIRESTORE 
firebase.initializeApp({
    projectId: FIREBASE_PROJECT_ID,
    databaseURL: FIREBASE_DATABASE_URL
  })
const db = firebase.firestore();

// CONFIGURE ALGOLIA 
const algolia = algoliasearch(
    ALGOLIA_APP_ID,
    ALGOLIA_API_KEY
  );
const index = algolia.initIndex.ALGOLIA_INDEX_NAME;


// INDEX FIRESTORE DATA INTO ALGOLIA 
var docRef = db.collection(ALGOLIA_INDEX_NAME);
const records = [];
db.collection(ALGOLIA_INDEX_NAME).get()
    .then((snapshot) => {
        snapshot.forEach((doc) => {
            // get the key and data from the snapshot
            const childKey = doc.id;
            const childData = doc.data();
            // We set the Algolia objectID as the Firebase .key
            childData.objectID = childKey;
            // Add object for indexing
            records.push(childData);
            console.log(doc.id, '=>', doc.data());
        });
        // Add or update new objects
        index.saveObjects(records).then(() => {
            console.log('Documents imported into Algolia');
            process.exit(0);
        })
        .catch(error => {
            console.error('Error when importing documents into Algolia', error);
            process.exit(1);
        });
    })
    .catch((err) => {
        console.error('Error getting documents', error);
    });



// SET UP REAL TIME CONNECTION BETWEEN ALGOLIA AND FIRESTORE 

// CALL ALGOLIA API ON THE CLIENT SIDE, FRONT END
// NEED TO SET UP PORT TO LISTEN TO THEN FOR LOCALHOST:3000/

0 个答案:

没有答案