Home > Enterprise >  Firebase functions crashes while accessing firestore
Firebase functions crashes while accessing firestore

Time:01-12

Could you please find an error in following code?

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();



exports.GetShort = functions.https.onRequest((request, response) => {
    response.header("Access-Control-Allow-Origin", "*");
    longURL = request.query.long
    functions.logger.info("url is - " ,longURL)
    SaveToDB(longURL)
})


function SaveToDB(link){
    functions.logger.info("here")
    admin.firestore().collection("url").where("urlNames","array_contains",link).get().then(
  
        function(querySnapshot){
            functions.logger.info("snap, " ,querySnapshot)
            querySnapshot.forEach(function(doc) {
            functions.logger.info("things :  " ,doc.id, " => ", doc.data())

                // doc.data() is never undefined for query doc snapshots
                console.log(doc.id, " => ", doc.data());
            });
        }
    ) .catch(function(error) {
        functions.logger.info("Error getting documents: ", error);
    });
}

After hitting above function, firebase-functions logs displays logs till "here". After that it crashes without any more logs/stacktrace.

below is the contents of packages.json from functions directory.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

CodePudding user response:

I would kindly suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series to see how to manage the life cycle of a Cloud Function and the way to deal with calls to asynchronous methods.

In particular, for an HTTPS Cloud Function, you need to end it with send(), redirect(), or end().

So your code could be adapted as follows:

exports.GetShort = functions.https.onRequest((request, response) => {
    response.header("Access-Control-Allow-Origin", "*");
    const longURL = request.query.long;
    functions.logger.info("url is - ", longURL)
    SaveToDB(longURL)
    .then(() => {
        response.status(200).send('Saved to DB');
    })
    .catch(error => {
        // See video series
        response.status(500).send(error);
    })
})


function SaveToDB(link) {
    functions.logger.info("here")
    return admin.firestore().collection("url").where("urlNames", "array_contains", link).get()
    .then(querySnapshot => {
        functions.logger.info("snap, ", querySnapshot)
        querySnapshot.forEach(function (doc) {
            functions.logger.info("things :  ", doc.id, " => ", doc.data())

            // doc.data() is never undefined for query doc snapshots
            console.log(doc.id, " => ", doc.data());
            
            // => Here, depending on your real functional requirements, you may need to use Promise.all()
            
        });
        return null;
    }
    ).catch(function (error) {
        functions.logger.info("Error getting documents: ", error);
        // Throw an error
    });
}

CodePudding user response:

Well, I found the problem. Sometimes silly things make most of the noise around. Instead of "array-contains", I wrote "array_contains".

  •  Tags:  
  • Related