node.js - Express 路由器删除和 Firebase Cloud Function 给出 TypeError: Cannot read property 'apply' of undefined
问题描述
我有一个带有 firebase.json 配置的 Firebase Cloud 功能:
{
"hosting":
{
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "api"
},
{
"source": "/*",
"function": "api"
}]
}
}
路由器.js
const express = require('express');
const router = express.Router();
const cors = require('cors')
const firebaseHandler = require('../db/firebaseHandler')
router.use(cors())
router.delete('/:id', async (req, res, next) => {
try {
const id = req.params.id;
if (!id) throw new Error('id is blank');
await firebaseHandler.delete('worklogs', id);
res.json({
id
});
} catch(e) {
next(e);
}
});
module.exports = router;
firebaseHandler.js
const firebase = require('firebase');
const conf = require('../../../conf.json')
const config = {
apiKey: conf.firebaseConfig.apiKey,
authDomain: conf.firebaseConfig.authDomain,
databaseURL: conf.firebaseConfig.databaseURL,
projectId: conf.firebaseConfig.projectId,
schemaPath: conf.schemaPath
};
firebaseApp = firebase.initializeApp(config);
const db = firebaseApp.firestore();
exports.delete = async (type, id) => {
await database.collection(type).doc(id).delete();
}
在 url 中运行它时出现错误:http://localhost:5000/mycloudfunctionhere/api/documentidhere/
这是错误的堆栈跟踪:
projectpath\node_modules\express\lib\router\index.js:635
return fn.apply(this, arguments);
TypeError: Cannot read property 'apply' of undefined
at Immediate.<anonymous> (projectpath\node_modules\express\lib\router\index.js:635:15)
at runCallback (timers.js:706:11)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
没有什么特别的,我已经尝试了多个示例,但我不断收到此错误..
提前致谢!
解决方案
答案在于您不知道您不能在浏览器 url 中发出删除请求。只要我做了一个 curl -x DELETE http://localhost:5000/mycloudfunctionhere/api/documentidhere/,它就起作用了
谢谢大家的回答!
推荐阅读
- python-3.x - 无法使用 numpy loadtxt 加载 csv 文件
- python - 使用通过表中的字段作为条件创建 ManyToManyField
- python-3.x - flask-错误的 URL 编码
- java - Firebase 性能插件不会在 Android 上构建
- scala - 如何访问可迭代元组(Scala)中的元素
- python - 如何将原始字符串转换为python3中的列表?
- android - 如何在不丢失 VCS 历史记录的情况下将代码从一个文件复制/粘贴到另一个文件
- html - 输入表单中的下拉菜单未选择
- angular - Angular7开发环境模块构造函数未运行
- java - 如何在运行时 Android Studio 期间更改小部件的样式