无法从Firebase集合删除文件

时间:2018-11-07 06:49:34

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

我遵循列出的示例here,但由于新firebase-tools的{​​{3}}而进行了修改。

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.

2 个答案:

答案 0 :(得分:1)

某些选项不能混用...

final := $(foreach F,$(files),$(lastword $(subst _, ,$F)))

这就是构建路径的方式(return firebase_tools.firestore.delete('flightApp3', path, { // allCollections: true, recursive: true, yes: true }).then(() => { return { path: path }; }); path似乎也没有意义):allCollections

getProjectId.js检查projects/${project}/databases/(default)/documents/users/${uid}/messages(其中rc.projects是选项options.project):

--project

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

.firebaserc

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


options.project解释了命令行选项:

$ firebase firestore:delete --help

npm package(上面的输出)的版本为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.


刚刚发现了一条相关评论(但可能已过时):

  

6.0.1必须在功能配置中设置,并且可以通过运行token在命令行中生成。

这暗示了environment configuration,因此firebase login:ci具有functions.config().fb.token

token

还可以从firebase functions:config:set fb.token="THE TOKEN" 获取projectId。

答案 1 :(得分:0)

文档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时不需要令牌。

使用FirestoreDelete的代码实现到API https://github.com/firebase/firebase-tools/blob/v7.16.2/src/firestore/delete.js的链接似乎也是错误的。

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