【问题标题】:Express router delete and Firebase Cloud Function gives TypeError: Cannot read property 'apply' of undefinedExpress router delete 和 Firebase Cloud Function 给出 TypeError: Cannot read property 'apply' of undefined
【发布时间】:2019-12-17 10:17:07
【问题描述】:

我有一个 Firebase Cloud 函数,其 firebase.json 配置为:

{
    "hosting":
    {
        "public": "public",
        "ignore": [
            "firebase.json",
            "**/.*",
            "**/node_modules/**"
        ],
        "rewrites": [
        {
            "source": "**",
            "function": "api"
        },
        {
            "source": "/*",
            "function": "api"
        }]
    }
}

我的灵感来自:this tutorialthis turorial

路由器.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)

没有什么具体的,我已经尝试了多个示例,但我不断收到此错误..

提前致谢!

【问题讨论】:

  • 请告诉哪里是像这样定义的数据库对象 admin.initializeApp({ credential: admin.credential.applicationDefault() }); const db = admin.firestore();
  • 代码中的哪一行导致了这个错误?为了提供帮助,我们需要的不仅仅是信息。查看错误的堆栈跟踪并找出哪一行来自您的文件。
  • 我已经用 firebaseHandler.js @MaheshBhatnagar 中的 firebase 配置更新了我的问题
  • @samthecodingman - 它没有说它来自巫婆线 - 除了 /myprojectpath/node_modules\express\lib\router\index.js:635 return fn.apply(this, arguments); > 类型错误:无法立即读取未定义的“应用”属性。 (myprojectpath\node_modules\express\lib\router\index.js:635:15) > 在 runCallback (timers.js:706:11) >在 tryOnImmediate (timers.js:676:5) > 在 processImmediate (timers.js:658:5)
  • 请使用此代码 await db.collection(''worklogs'').doc(id).delete();

标签: node.js firebase express google-cloud-functions express-router


【解决方案1】:

答案在于您不知道您不能在浏览器 url 中发出删除请求。只要我做了一个 curl -x DELETE http://localhost:5000/mycloudfunctionhere/api/documentidhere/

,它就起作用了

谢谢大家的回答!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2021-06-25
    • 2017-08-19
    • 1970-01-01
    • 2017-04-24
    • 2023-03-15
    相关资源
    最近更新 更多