【问题标题】:algolia firestore function wont triggeralgolia firestore 功能不会触发
【发布时间】:2018-03-14 21:23:42
【问题描述】:

我想将我的 Cloud Firestore 与 Algolia 搜索索引同步。我目前只能部署 firestore 功能,但它不会触发 onWrite 或 onChange。 我正在关注本指南:Angular Full Text Search With Algolia Backend

如您所见,JS 已部署,但当我在数据库中添加、删除或更改文档时,它不会触发。日志也不显示。

数据库设计:

-|search
    -|searchId
        -|id: string
        -|name: sting
        -|ref: string

数据库规则:

//Search field
match /search/{searchId}{
    allow read;
  allow write: if request.auth != null;
}

JS函数:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const algoliasearch = require('algoliasearch');
const algolia = algoliasearch(functions.config().algolia.appid, functions.config().algolia.adminkey);


exports.updateIndex = functions.database.ref('/search/{searchId}').onWrite(event => {

  const index = algolia.initIndex('search');

  const searchId = event.params.searchId
  const data = event.data.val()

  if (!data) {
    return index.deleteObject(searchId, (err) => {
      if (err) throw err
      console.log('Search Removed from Algolia Index', searchId)
    })

  }

  data['objectID'] = searchId

  return index.saveObject(data, (err, content) => {
    if (err) throw err
    console.log('Search Updated in Algolia Index', data.objectID)
  })

});

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    我遇到了类似的问题。更改这些行:

    exports.updateIndex = functions.database.ref('/search/{searchId}')...

    exports.updateIndex = functions.firestore.document('search/{searchId}')...


    const data = event.data.val()

    const data = event.data.exists ? event.data.data() : null;

    它应该可以工作。它对我有用,但我现在面临
    Function returned undefined, expected Promise or value 日志错误,无法解决,但功能有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2020-10-15
      • 2021-09-14
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多