【问题标题】:Error Deploying Firestore Function with a space in the name of a collection使用集合名称中的空格部署 Firestore 函数时出错
【发布时间】:2018-04-12 20:05:12
【问题描述】:

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

错误信息:

=== 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
}

【问题讨论】:

    标签: firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

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

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

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

      product.objectID = event.params.postId;
    

    【讨论】:

    • 所以在我的 Firestore 中,我有一个名为“产品目录”的集合,这就是我这样引用它的原因。很高兴知道即使您可以在控制台中执行此操作,也不能以这种方式引用它。该函数已部署但未执行,我认为它与 {id} 有关。我会单独解决这个问题。感谢您的帮助!
    • 我在路径中遗漏了一个“/”,这个答案帮助我指明了正确的方向。
    • 如果您错误地尝试从文档路径中引用对象上的集合或属性,也会引发相同的错误。
    • 我的是一个较长的片段 "/proxies/countries/united states/states/.../cities/... 我继续将美国参数化为 {country} ``` if (context .params.country !== '美国') { return; } ```
    【解决方案2】:

    我认为这是 Firebase CLI 部署中的一个错误。集合 ID 和文档 ID 中的空格在 Firestore 中完全有效,如果存在空格,部署不应失败。我已经在 Firebase 团队内部记录了一个关于此问题的错误,您应该随时自行记录任何看起来像错误的内容at this page

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2022-09-29
      • 2019-02-14
      • 2022-01-09
      • 1970-01-01
      • 2016-03-04
      相关资源
      最近更新 更多