#Firebase cloud functions testing

1 messages · Page 1 of 1 (latest)

frank coral
#

Hey guys I have this cloud function that mirrors data to algolia on firebase write.

It works fine but now I wanted to add this:
https://www.algolia.com/doc/guides/managing-results/optimize-search-results/override-search-engine-defaults/how-to/how-can-i-make-queries-within-the-middle-of-a-word/?client=javascript

Possibility to query the words in the middle, and the way this is achieved is by breaking down property into substring array. With the following proposed method:

let objects = require("./products.json");

objects = objects.map(record => {
  let reference = record.product_reference;
  record.product_reference_suffixes = [];

  while (reference.length > 1) {
    reference = reference.substr(1);
    record.product_reference_suffixes.push(reference);
  }

  return record;
});

index.saveObjects(objects).then(() => {
  // done
});

So here is my attempt to add it to the existing function of mirroring to post-process property called title. I have it commented out because it breaks the function completely and I am really unsure how to debug this. Is there something obvious I am doing wrong, with the commented-out part?

exports.indexentry = functions.database.ref('/products/{blogid}').onWrite(
  async (data, context) => {


    
    // let reference = data.after.title;
    // record.product_reference_suffixes = [];
  
    // while (reference.length > 1) {
    //   reference = reference.substr(1);
    //   data.after.product_reference_suffixes.push(reference);
    // }
  
    const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
    const firebaseObject = {
      data: data.after.val(),
      objectID: context.params.blogid
    };
  if (!firebaseObject.data) {
      return index.deleteObject(firebaseObject.objectID)}

    await index.saveObject(firebaseObject);

  });


#

So yeah long story short I need to break down specific property "title" that arrives under "data" into substrings like such:

 "title": "002ABCDEF",
    "product_reference_suffixes": ["02ABCDEF", "2ABCDEF", "ABCDEF", "BCDEF", "CDEF", "DEF", "EF", "F"]

frank coral
#

solved with a bunch of console logs and patience

plain geyser
#

took only 6 hours 😉