【问题标题】:Cannot delete files from firebase collection无法从 Firebase 集合中删除文件
【发布时间】:2019-04-10 14:27:39
【问题描述】:

我遵循here 列出的示例,但由于新firebase-toolsAPI 有所修改。

exports.clearMessages = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).https.onCall(messagesController.clearMessages)

export const clearMessages = async (data, context) => {
    const uid = context.auth.uid
    const path = `users/${uid}/messages`
    return firebase_tools.firestore.delete('flightApp3', path, {
        recursive: true,
        shallow: true,
        allCollections: true
    }).then(result => {
        console.log('delete result', result)
        return result
    })
}

但是,当我运行它时,我看到 Cloud Functions 日志中显示以下内容:

Unhandled error { Error
    at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)
    at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)
    at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)
    at /user_code/node_modules/firebase-tools/lib/command.js:154:38
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
  name: 'FirebaseError',
  message: 'No project active. Run with \u001b[1m--project <projectId>\u001b[22m or define an alias by\nrunning \u001b[1mfirebase use --add\u001b[22m',
  children: [],
  status: 500,
  exit: 1,
  stack: 'Error\n    at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)\n    at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)\n    at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)\n    at /user_code/node_modules/firebase-tools/lib/command.js:154:38\n    at process._tickDomainCallback (internal/process/next_tick.js:135:7)',
  original: undefined,
  context: undefined }

但是,我很确定我的 firebase CLI 中有一个活动项目。

$ firebase use
Active Project: production (flightApp3)

Project aliases for /Users/myUser/Developer/flightApp3/cloud:

* default (flightApp3)
* production (flightApp3)

Run firebase use --add to define a new project alias.

【问题讨论】:

  • 这似乎不是一个完整的例子。你怎么打电话给clearMessagesFIREBASE_PROJECT_ID 是什么?
  • @DougStevenson 已编辑
  • 我认为您的本地项目不会延续到 Cloud Functions 环境,但不确定如何在调用中传递项目 ID。我去问问。

标签: node.js firebase google-cloud-firestore firebase-tools


【解决方案1】:

有些选项不能混用...

return firebase_tools.firestore.delete('flightApp3', path, {
    // allCollections: true,
    recursive: true,
    yes: true
}).then(() => {
    return {
      path: path 
    };
});

这就是构建路径的方式(pathallCollections 似乎也没有意义):projects/${project}/databases/(default)/documents/users/${uid}/messages

getProjectId.js 检查rc.projects(其中options.project 是选项--project):

module.exports = function(options, allowNull) {
    if (!options.project && !allowNull) {
        var aliases = _.get(options, "rc.projects", {});
        ...

这些rc.projects 是来自.firebaserc 文件的projects

{
   "projects": {
        "default": "flightApp3"
    }
}

或运行firebase use default 以从别名production 切换到default(或删除别名production 一次以进行测试)。 FirestoreDelete(project, path, options) 也不再关心options.tokenoptions.project(正如documentation 所建议的那样)。


$ firebase firestore:delete --help 解释命令行选项:

Usage: firestore:delete [options] [path]

Delete data from Cloud Firestore.

Options:

  -r, --recursive    Recursive. Delete all documents and sub-collections. 
                     Any action which would result in the deletion of child
                     documents will fail if this argument is not passed.

                     May not be passed along with --shallow.


  --shallow          Shallow. Delete only parent documents and ignore documents
                     in sub-collections. Any action which would orphan documents
                     will fail if this argument is not passed.

                     May not be passed along with --recursive.


  --all-collections  Delete all. Deletes the entire Firestore database,
                     including all collections and documents.

                     Any other flags or arguments will be ignored.


  -y, --yes          No confirmation. Otherwise, a confirmation prompt will appear.

npm package(上面的输出)的版本是6.0.1


刚刚找到一条相关评论(但可能已过时):

token 必须在函数配置中设置,并且可以在命令行通过运行firebase login:ci 生成。

这提示environment configuration,因此functions.config().fb.token 具有token

firebase functions:config:set fb.token="THE TOKEN"

也可以从process.env.FIREBASE_CONFIG.projectId获取projectId。

【讨论】:

  • 文档已过期,与最近firebase-tools的API不匹配
【解决方案2】:

文档https://firebase.google.com/docs/firestore/solutions/delete-collections

/functions目录下安装firebase-tools

    "firebase-tools": "^7.16.2"

在云函数中导入firebase-tools并调用delete

const firebaseTools = require("firebase-tools");
...
firebaseTools.firestore.delete(workspaceRef.path, {
                project: process.env.GCLOUD_PROJECT, // required
                recursive: true, // required
                yes: true // required
              })

从云函数调用firebase-tools 时不需要令牌。

此外,API https://github.com/firebase/firebase-tools/blob/v7.16.2/src/firestore/delete.js 的链接以及 FirestoreDelete 的代码实现似乎是错误的。

我已成功调用.delete(path, options),但代码显示.delete(project, path, options)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 2020-08-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多