我正在使用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,
})
});
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:Userspjsupportfolioreact-firebase-boilerplatefunctions>
似乎没有任何错误信息告诉我什么是错的,进一步我密切关注这里的消防船视频:https://www.youtube.com/watch?v=3Z0V3cvgns8&ab_channel=Fireship
有什么办法解决这个问题吗?
看起来失败是由于你的函数代码中的错误。
- indexHomeCare -要添加对象到索引,您必须使用saveObject
- 裁判:https://www.algolia.com/doc/api-reference/api-methods/add-objects/
- removeHomeCare -要从索引中删除对象,将对象ID传递为字符串值/或在数组中多次删除。
- 裁判:https://www.algolia.com/doc/api-reference/api-methods/delete-objects/
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
则集合为";contractors&;";{通配符}是集合中的所有文档。