首页 > 解决方案 > 简单的云功能复制文档时出错

问题描述

我正在创建我的第一个云函数,它只是在创建journal子集合activites后将子集合中的文档复制到子集合中。我希望复制的文档具有相同的postId. 功能日志显示

Error: Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.
at Object.validateResourcePath (/workspace/node_modules/@google-cloud/firestore/build/src/path.js:406:15)
at CollectionReference.doc (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1982:20)
at exports.onCreateJournal.functions.firestore.document.onCreate (/workspace/index.js:21:8)
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:134:23)
at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:198:28)
at process._tickCallback (internal/process/next_tick.js:6

这是云功能

exports.onCreateJournal = functions.firestore
  .document("/users/{userId}/journal/{docID}")
  .onCreate(async (snapshot, context) => {
    const data = snapshot.data();
    const userId = context.params.userId;
    const postId = context.params.postId;

    admin
      .firestore()
      .collection("users")
      .doc(userId)
      .collection("activities")
      .doc(postId)
      .set(data);
  });

或者我应该用 Firestore 模拟器调试这个?

标签: javascriptgoogle-cloud-firestoregoogle-cloud-functions

解决方案


错误信息:

错误:参数“documentPath”的值不是有效的资源路径。路径必须是非空字符串。

建议您将空字符串或其他一些无效值(例如未定义)传递给您对doc(). 我的猜测是第二个。

Your wildcard path has "docID" in it, but you are trying to access it with a different name "postId". You will need to use the same name for both. Perhaps you meant to use a wildcard like this:

exports.onCreateJournal = functions.firestore
  .document("/users/{userId}/journal/{postId}")

Notice that I replaced "docID" with "postId" to match your code.


推荐阅读