简体   繁体   English

Firebase 功能:与 Algolia 同步不起作用

[英]Firebase functions: sync with Algolia doesn't work

I am currently trying to sync my firestore documents with algolia upon a new document creation or the update of a document.我目前正在尝试在创建新文档或更新文档时将我的 firestore 文档与 algolia 同步。 The path to the collection in firestore is videos/video. firestore 中集合的路径是 videos/video。 The function seems to be triggering fine, however after triggering, the firebase function does not seem to relay any of the information to algolia (no records are being created). function 似乎触发良好,但触发后,firebase function 似乎没有将任何信息传递给 algolia(没有创建记录)。 I am not getting any errors in the log.我没有在日志中收到任何错误。 (I also double checked the rules and made sure the node could be read by default, and yes I am on the blaze plan). (我还仔细检查了规则并确保默认情况下可以读取节点,是的,我在 blaze 计划中)。 Does anyone know how to sync a firestore node and algolia?有谁知道如何同步 firestore 节点和 algolia? Thanks for all your help!感谢你的帮助!

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

Still don't know what I did wrong, however if anyone else is stuck in this situation, this repository is a great resource: https://github.com/nayfin/algolia-firestore-sync .仍然不知道我做错了什么,但是如果其他人遇到这种情况,这个存储库是一个很好的资源: https://github.com/nayfin/algolia-firestore-sync I used it and was able to properly sync firebase and algolia.我使用它并能够正确同步 firebase 和 algolia。 Cheers!干杯!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM