首页 > 解决方案 > algolia 全文搜索,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>

似乎没有任何错误消息告诉我出了什么问题,我进一步密切关注了这里的 fireship 视频:https ://www.youtube.com/watch?v=3Z0V3cvgns8&ab_channel=Fireship

任何想法如何解决这一问题?

标签: reactjsgoogle-cloud-firestoregoogle-cloud-functionsfull-text-searchalgolia

解决方案


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

  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);
  });

推荐阅读