首页 > 解决方案 > 无法从 Firebase 集合中删除文件

问题描述

我正在按照此处列出的示例进行操作,但由于新的APIfirebase-tools进行了修改。

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

但是,当我运行 this 时,我会在 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.

标签: node.jsfirebasegoogle-cloud-firestorefirebase-tools

解决方案


有些选项不能混合...

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

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

getProjectId.js检查rc.projectsoptions.project选项在哪里--project):

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

这些rc.projectsprojects来自.firebaserc文件:

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

或运行firebase use default以从别名切换productiondefault(或删除别名production一次以进行测试)。FirestoreDelete(project, path, options)也不再关心options.tokenoptions.project不再关心(如文档所示)。


$ 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 包(上面的输出)是 version 6.0.1


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

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

这提示了环境配置,因此functions.config().fb.token具有token

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

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


推荐阅读