0

I have a Firebase Cloud function with firebase.json config as:

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

I have been inspired by: this tutorial and this turorial

router.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(); 
}

I'm getting the error when running this in the url: http://localhost:5000/mycloudfunctionhere/api/documentidhere/

Here is the stack trace of the error:

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)

Nothing specific and I have tried out multiple examples but I keep getting this error..

Thanks in advance!

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Please tell where is database object defined like that admin.initializeApp({ credential: admin.credential.applicationDefault() }); const db = admin.firestore(); – Mahesh Bhatnagar Dec 17 '19 at 10:23
  • Which specific line in your code is causing this error? To help we will need more than just the message. Take a look at the error's stack trace and find which line comes from one of your files. – samthecodingman Dec 17 '19 at 10:52
  • I have updated my question with firebase configuration in firebaseHandler.js @MaheshBhatnagar – Louise Nielsen Dec 17 '19 at 11:02
  • @samthecodingman - It doesn't say witch line it comes from - other than /myprojectpath/node_modules\express\lib\router\index.js:635 return fn.apply(this, arguments); > TypeError: Cannot read property 'apply' of undefined > at Immediate. (myprojectpath\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) – Louise Nielsen Dec 17 '19 at 11:03
  • Please use this code await db.collection(''worklogs'').doc(id).delete(); – Mahesh Bhatnagar Dec 17 '19 at 11:11
  • @MaheshBhatnagar - I've tried: await firebaseHandler.delete(id); and await db.collection("worklogs").doc(id).delete();. Still getting the error :-/ – Louise Nielsen Dec 17 '19 at 11:16
  • can you share your full code if no problem you – Mahesh Bhatnagar Dec 17 '19 at 11:18
  • It is like the router.delete() method does not work. I've even tried to just console.log("hello") on my router.delete(/:id, () => ) method. Still getting the error. @MaheshBhatnagar - There is no more code than this I have posted here now – Louise Nielsen Dec 17 '19 at 11:19
  • ok sir, i am checking that issue, Please tell me, are you sure that you got id value in function – Mahesh Bhatnagar Dec 17 '19 at 11:22
  • And tried the delete method from here: https://expressjs.com/en/starter/basic-routing.html . And trying http://localhost:5000/cloudfunction/api/user . Same error.. – Louise Nielsen Dec 17 '19 at 11:25
  • Can you post the code where you attach the router to the Cloud Function? – samthecodingman Dec 17 '19 at 11:28
  • Is that the complete `router.js` file? Aren't you missing `module.exports = router` at the bottom of the file? – samthecodingman Dec 17 '19 at 11:31
  • @samthecodingman - Sorry, I do have that line in my code (added it to my question now) – Louise Nielsen Dec 17 '19 at 11:34
  • Please use this query router.post('/', async (req, res, next) => { console.log("hello"); }); and comment of delete function & router of – Mahesh Bhatnagar Dec 17 '19 at 11:40
  • @MaheshBhatnagar: That gives me the same error. But it is working perfectly fine with router.get('/', async (req, res, next) => { console.log("hello"); }); – Louise Nielsen Dec 17 '19 at 11:51
  • Please share full code – Mahesh Bhatnagar Dec 17 '19 at 11:56

1 Answers1

1

The answer lies in the lack of knowledge that you cannot make a delete request in a browser url. It worked as soon as I made a curl -x DELETE http://localhost:5000/mycloudfunctionhere/api/documentidhere/

Thank you for your answers everyone!