node.js - 无法从 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.
解决方案
有些选项不能混合...
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.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
或options.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
。
推荐阅读
- c++ - 什么可能导致Ncurses混乱中的坐标
- python - 关于将 sum() 与 zip() 结合起来的问题
- asp.net-mvc - Azure App Service 授权/easy auth 与 ASP.NET Forms auth 冲突
- angular - 如何使用 window.opener 从 Angular 8 中的子窗口获取父组件数据
- javascript - 在vue中上传和处理excel文件
- php - Stripe PaymentIntent PHP client_secret 解析为 null
- discord.js - 重写命令处理程序
- laravel - 调用未定义的方法 Illuminate\Database\Query\Builder::firstOrFail()
- flutter - 如何在 Flutter Flame 游戏引擎中停止精灵动画
- c++ - 计算二叉搜索树级别中的节点数