我想将我的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)
})
});
答案 0 :(得分:0)
我遇到了类似的问题。改变这些行:
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
日志错误,无法解决,但功能正常。