简体   繁体   中英

Get Value Key pair from Firebase with Node.js function

Ok, I am using Algolia for handling search within my app. The following code adds whatever is in my database to my Algolia Index. However, whenever the data is imported, all of the values in a firebase node get placed under "text" in the Algolia Index.

How can make it to where it is stored as the key value pair found in Firebase.

ie

address: 1234 Main St.

VS. / (Instead of)

text:

address: 1234 Main St.


My Code:

exports.indexentry = functions.database
  .ref('/blog-posts/{blogid}/text')
  .onWrite(event => {
    const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
    const firebaseObject = {
      text: event.data.val(),
      objectID: event.params.blogid,
    };
  });

在此处输入图片说明

So what you are missing here, is the pushing of the data to Algolia. What you can do if you want to have all the firebase data in an object, is assigning it to a new object, as well as the objectID

exports.indexentry = functions.database
  .ref('/blog-posts/{blogid}/text')
  .onWrite(event => {
    const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
    const firebaseObject = Object.assign({}, event.data.val(), {
      objectID: event.params.blogid,
    });
    index.saveObject(firebaseObject); // .then or .catch as well
  });

Does that make sense?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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