EN VI

Javascript - Firebase function says document doesn not exist. What am I doing wrong?

2024-03-17 06:30:07
Javascript - Firebase function says document doesn not exist. What am I doing wrong?

I am very new to Firebase functions. I wrote the following function which I expect to return a document by its ID. The document with that ID does exist. In the following code however, it always returns "Quiz not found". Am I doing something wrong?

// The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
const { logger } = require("firebase-functions");
const { onRequest } = require("firebase-functions/v2/https");

// The Firebase Admin SDK to access Firestore.
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");

initializeApp();

exports.getQuizById = onRequest(async (request, response) => {
    const quizId = request.query.id;

    if (!quizId) {
        response.status(400).send('Quiz ID is required');
        return;
    }

    try {
        const quizDoc = await getFirestore().collection('quizzes').doc(quizId).get();

        if (!quizDoc.exists) {
            response.status(404).send('Quiz not found. ID: ' + quizId);
            return;
        }

        response.status(200).json(quizDoc.data());
    } catch (error) {
        console.error('Error getting quiz document:', error);
        response.status(500).send('Internal Server Error');
    }
});

Solution:

please try the following rearrangements.

// The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
const { logger } = require("firebase-functions");
const { onRequest } = require("firebase-functions/v2/https");

// The Firebase Admin SDK to access Firestore.
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");


initializeApp();

const db = getFirestore(); //<--should init here after  initializeApp()

exports.getQuizById = onRequest(async (request, response) => {
    const quizId = request.query.id;

    if (!quizId) {
        response.status(400).send('Quiz ID is required');
        return;
    }

    try {
      // await getFirestore().collection('quizzes').doc(quizId).get();
      // should works just fine also but the below style could be more helpul if your code become more complex.
        const quizDocref =  db.collection('quizzes').doc(quizId); 
        const quizDoc= await quizDocref.get();

        if (!quizDoc.exists) {
            response.status(404).send('Quiz not found. ID: ' + quizId);
            return;
        }

        response.status(200).json(quizDoc.data());
    } catch (error) {
        console.error('Error getting quiz document:', error);
        response.status(500).send('Internal Server Error');
    }
});
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login