无法从firebase集合中删除文件

TIM*_*MEX 10 node.js firebase firebase-tools google-cloud-firestore

我遵循此处列出的示例,除非由于新API的修改firebase-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
    })
}
Run Code Online (Sandbox Code Playgroud)

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

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 }
Run Code Online (Sandbox Code Playgroud)

但是,我很确定我的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.
Run Code Online (Sandbox Code Playgroud)

Mar*_*ler 2

有些选项不能混合...

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

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

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

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

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

{
   "projects": {
        "default": "flightApp3"
    }
}
Run Code Online (Sandbox Code Playgroud)

或运行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.
Run Code Online (Sandbox Code Playgroud)

npm(上面的输出)位于 version 6.0.1


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

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

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

firebase functions:config:set fb.token="THE TOKEN"
Run Code Online (Sandbox Code Playgroud)

还可以从 获取projectId process.env.FIREBASE_CONFIG.projectId