Node.js 快速路由器删除和Firebase云函数给出TypeError:无法读取属性';应用';未定义的
我有一个Firebase Cloud函数,其中Firebase.json配置为:Node.js 快速路由器删除和Firebase云函数给出TypeError:无法读取属性';应用';未定义的,node.js,firebase,express,google-cloud-functions,express-router,Node.js,Firebase,Express,Google Cloud Functions,Express Router,我有一个Firebase Cloud函数,其中Firebase.json配置为: { "hosting": { "public": "public", "ignore": [ "firebase.json", "**/.*", "**/node_mo
{
"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\u modules\express\lib\router\index.js:635
返回fn.apply(这是参数);
TypeError:无法读取未定义的属性“apply”
马上。(projectpath\node\u modules\express\lib\router\index.js:635:15)
运行回调时(timers.js:706:11)
在tryOnImmediate(timers.js:676:5)
在processImmediate(timers.js:658:5)
没有什么特别的,我已经尝试了多个例子,但我一直得到这个错误
提前谢谢 答案在于您不知道不能在浏览器url中发出删除请求。我一做curl-x删除,它就工作了
谢谢大家的回答 请告诉像admin.initializeApp({credential:admin.credential.applicationDefault()})这样定义的数据库对象在哪里;const db=admin.firestore();代码中的哪一行导致此错误?为了提供帮助,我们需要的不仅仅是信息。查看错误的堆栈跟踪,找出哪一行来自您的一个文件@MaheshBhatnagar@samthecodingman-它没有说它来自于哪个行-除了/myprojectpath/node\u modules\express\lib\router\index.js:635 return fn.apply(这是参数);>TypeError:无法在立即读取undefined>的属性“apply”。(myprojectpath\node\U modules\express\lib\router\index.js:635:15)>在runCallback(timers.js:706:11)>在tryOnImmediate(timers.js:676:5)>在processImmediate(timers.js:658:5)请使用此代码等待db.collection(“'worklogs”).doc(id).delete();