【问题标题】:Firebase functions: sync with Algolia doesn't workFirebase 功能:与 Algolia 同步不起作用
【发布时间】:2020-05-28 12:29:01
【问题描述】:

我目前正在尝试在创建新文档或更新文档时将我的 firestore 文档与 algolia 同步。 firestore 中收藏的路径是视频/视频。该功能似乎触发良好,但触发后,firebase 功能似乎没有将任何信息传递给 algolia(没有创建记录)。我在日志中没有收到任何错误。 (我还仔细检查了规则并确保默认情况下可以读取节点,是的,我正在制定计划)。有谁知道如何同步firestore节点和algolia?感谢您的所有帮助!

"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const algoliasearch_1 = require("algoliasearch");
// Set up Firestore.
admin.initializeApp();
const env = functions.config();
// Set up Algolia.
// The app id and API key are coming from the cloud functions environment, as we set up in Part 1,
const algoliaClient = algoliasearch_1.default(env.algolia.appid, env.algolia.apikey);
// Since I'm using develop and production environments, I'm automatically defining 
// the index name according to which environment is running. functions.config().projectId is a default property set by Cloud Functions.
const collectionindexvideo = algoliaClient.initIndex('videos');

exports.collectionvideoOnCreate = functions.firestore.document('videos/{uid}').onCreate(async(snapshot, context) => {
  await savevideo(snapshot);
});
exports.collectionvideoOnUpdate = functions.firestore.document('videos/{uid}').onUpdate(async(change, context) => {
  await updatevideo(change);
});
exports.collectionvideoOnDelete = functions.firestore.document('videos/{uid}').onDelete(async(snapshot, context) => {
  await deletevideo(snapshot);
});
async function savevideo(snapshot) {
  if (snapshot.exists) {
    const document = snapshot.data();
    // Essentially, you want your records to contain any information that facilitates search, 
    // display, filtering, or relevance. Otherwise, you can leave it out.
    const record = {
      objectID: snapshot.id,
      uid: document.uid,
      title: document.title,
      thumbnailurl: document.thumbnailurl,
      date: document.date,
      description: document.description,
      genre: document.genre,
      recipe: document.recipe
    };
    if (record) { // Removes the possibility of snapshot.data() being undefined.
      if (document.isIncomplete === false) {
        // In this example, we are including all properties of the Firestore document 
        // in the Algolia record, but do remember to evaluate if they are all necessary.
        // More on that in Part 2, Step 2 above.
        await collectionindexvideo.saveObject(record); // Adds or replaces a specific object.
      }
    }
  }
}
async function updatevideo(change) {
  const docBeforeChange = change.before.data();
  const docAfterChange = change.after.data();
  if (docBeforeChange && docAfterChange) {
    if (docAfterChange.isIncomplete && !docBeforeChange.isIncomplete) {
      // If the doc was COMPLETE and is now INCOMPLETE, it was 
      // previously indexed in algolia and must now be removed.
      await deletevideo(change.after);
    } else if (docAfterChange.isIncomplete === false) {
      await savevideo(change.after);
    }
  }
}
async function deletevideo(snapshot) {
  if (snapshot.exists) {
    const objectID = snapshot.id;
    await collectionindexvideo.deleteObject(objectID);
  }
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions algolia


    【解决方案1】:

    仍然不知道我做错了什么,但是如果其他人陷入这种情况,这个存储库是一个很好的资源:@​​987654321@。我使用它并且能够正确同步 firebase 和 algolia。干杯!

    // Takes an the Algolia index and key of document to be deleted
    const removeObject = (index, key) => {
      // then it deletes the document
      return index.deleteObject(key, (err) => {
        if (err) throw err
        console.log('Key Removed from Algolia Index', key)
      })
    }
    // Takes an the Algolia index and data to be added or updated to
    const upsertObject = (index, data) => {
      // then it adds or updates it
      return index.saveObject(data, (err, content) => {
        if (err) throw err
        console.log(`Document ${data.objectID} Updated in Algolia Index `)
      })
    }
    
    exports.syncAlgoliaWithFirestore = (index, change, context) => {
      const data = change.after.exists ? change.after.data() : null;
      const key = context.params.id; // gets the id of the document changed
      // If no data then it was a delete event
      if (!data) {
        // so delete the document from Algolia index
        return removeObject(index, key);
      }
      data['objectID'] = key;
      // upsert the data to the Algolia index
      return upsertObject(index, data);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多