algolia 全文搜索,firebase

algolia full text search, firebase

我正在使用 firebase 样板文件: https://github.com/WataruMaeda/react-firebase-boilerplate

并立即尝试添加全文搜索。

代码

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const env = functions.config();

const algoliasearch = require("algoliasearch");

var client = algoliasearch(env.algolia.appid, env.algolia.apikey);
const index = client.initIndex("Moseley");

exports.indexHomecare = functions.firestore
  .document("contractors")
  .onCreate((snap, context) => {
    const data = snap.data()
    const objectId = snap.id
    return index.addObject({
      objectId,
      ...data,
    })
  });

exports.removeHomeCare = functions.firestore
  .document("contractors")
  .onDelete((snap, context) => {
    const objectId = snap.id
    return index.deleteObject({
      objectId,
    })
  });

错误信息

+  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
+  functions: required API cloudbuild.googleapis.com is enabled
+  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (43.24 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: uploading functions in project: indexHomecare(europe-west2), removeHomeCare(europe-west2)
i  functions: creating Node.js 12 function indexHomecare(europe-west2)...
i  functions: creating Node.js 12 function removeHomeCare(europe-west2)...
!  functions: failed to create function removeHomeCare
HTTP Error: 400, The request has errors
!  functions: failed to create function indexHomecare
HTTP Error: 400, The request has errors


Functions deploy had errors with the following functions:
        indexHomecare
        removeHomeCare


To try redeploying those functions, run:
    firebase deploy --only "functions:indexHomecare,functions:removeHomeCare"


To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.

Having trouble? Try firebase [command] --help

C:\Users\pjsup\portfolio\react-firebase-boilerplate\functions>

似乎没有任何错误消息告诉我哪里出了问题,而且我已经密切关注这里的火船视频:https://www.youtube.com/watch?v=3Z0V3cvgns8&ab_channel=Fireship

有什么解决办法吗?

看起来失败是由于您的函数代码中的错误。

  1. indexHomeCare - 要将对象添加到索引,您必须使用 saveObject
  1. removeHomeCare - 要从索引中删除对象,将对象 ID 作为字符串值传递/或在数组中进行多次删除。
exports.indexHomecare = functions.firestore
  .document("contractors")
  .onCreate((snap, context) => {
    const data = snap.data()
    const objectId = snap.id
    return index.saveObject({
      objectId,
      ...data,
    })
  });

exports.removeHomeCare = functions.firestore
  .document("contractors")
  .onDelete((snap, context) => {
    const objectId = snap.id
    return index.deleteObject(objectId);
  });
  .document("contractors")

需要

  .document("contractors/{wildcard)")

https://firebase.google.com/docs/functions/firestore-events

集合是“承包商”,{wildcard} 是“集合中的所有文档。