我有一个Firebase Cloud函数,Firebase.json配置为:
{
"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;
火碱处理程序.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/
以下是错误的堆栈跟踪:
projectpathnode_modulesexpresslibrouterindex.js:635
return fn.apply(this, arguments);
TypeError: Cannot read property 'apply' of undefined
at Immediate.<anonymous> (projectpathnode_modulesexpresslibrouterindex.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/,它就起作用
了谢谢大家的回答!