使用集合名称中的空格部署 Firestore 函数时出错 [英] Error Deploying Firestore Function with a space in the name of a collection

查看:15
本文介绍了使用集合名称中的空格部署 Firestore 函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试部署一个云函数,每当在我的 Firestore 中创建新文档时,该函数就会更新 Algolia 搜索索引.一切似乎开始都很好,但到了最后,却出错了.我已经阅读了文档,感觉我的语法是正确的,所以我不知道此时该怎么做.Cloud Functions 日志没有指向任何内容,只是回显了我在下面收到的错误

I'm trying to deploy a cloud function that updates an Algolia Search index whenever a new document is created in my Firestore. Everything seems to start well but then at the very end, it errors out. I've been through the docs and it feels like my syntax is correct so I'm not sure what to do at this point. The Cloud Functions log don't point to anything and just echo the error that I received below

错误信息:

=== Deploying to 'test-ffdbb'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
+  runtimeconfig: all necessary APIs are enabled
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (1.25 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating function onProductCreated...
!  functions[onProductCreated]: Deployment error.
Failed to configure trigger providers/cloud.firestore/eventTypes/document.create@firestore.googleapis.com (onProductCreated)

我的代码:

const functions = require('firebase-functions');
const algoliasearch = require('algoliasearch');

// App ID and API Key are stored in functions config variables
const ALGOLIA_ID = functions.config().algolia.app_id;
const ALGOLIA_ADMIN_KEY = functions.config().algolia.api_key;

const ALGOLIA_INDEX_NAME = "Firestore";
const client = algoliasearch(ALGOLIA_ID, ALGOLIA_ADMIN_KEY);

// Update the search index every time a blog post is written.
exports.onProductCreated = functions.firestore
.document("Product Catalog/{id}")
.onCreate(event => {
  const product = event.data.data();
  product.objectID = event.params.postId;

  const index = client.initIndex(ALGOLIA_INDEX_NAME);
  return index.saveObject(product);
});

package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "@google-cloud/firestore": "^0.8.2",
    "algoliasearch": "^3.24.5",
    "firebase-admin": "~5.4.2",
    "firebase-functions": "^0.7.1"
  },
  "private": true
}

推荐答案

问题似乎是路径段Product Catalog中的空格.当我删除它时,该函数部署成功:

The problem appears to be the space in path segment Product Catalog. When I remove it, the function deploys successfully:

exports.onProductCreated = functions.firestore
  .document("ProductCatalog/{id}") // <== space removed
  .onCreate(event => {...});

另请注意,您的路径中的通配符是 id 并且在您引用的代码中是 postId:

Also note that the wildcard in your path is id and in the code you reference postId:

  product.objectID = event.params.postId;

这篇关于使用集合名称中的空格部署 Firestore 函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆