firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.61k stars 364 forks source link

Firestore Repeatedly Returning Error: 5 NOT_FOUND #2563

Closed sr5434 closed 3 months ago

sr5434 commented 3 months ago

[READ] Step 1: Are you in the right place?

Cloud Firestore support is provided by the @google-cloud/firestore library. Therefore the easiest and most efficient way to get Firestore issues resolved is by directly reporting them at the nodejs-firestore GitHub repo.

If you still think the problem is related to the code in this repository, then read on.

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

When I call the API Route, instead of creating a new user in Firestore, I get a vague "Error: 5 NOT_FOUND" error message with no additional details. I have used this code before in other projects and can confirm that it used to work.

Relevant Code:

The following is a NextJS API Route I used.

import { NextResponse, Response } from 'next/server'

import { getFirestore } from "firebase-admin/firestore";
import * as admin from 'firebase-admin'

const serviceAccount = require('../../../service_account.json');
if (admin.apps.length === 0) {
  admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://database.firebaseio.com'
  });
}
const db = getFirestore();

export async function POST(req){
  const {userId} = getAuth(req);
  const usersDb = db.collection('users');
  await usersDb.doc("example-user-id").set({
    customers: []
  });
  return NextResponse.json({ result: "Success" });
}
google-oss-bot commented 3 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

lahirumaramba commented 3 months ago

Did you enable Firestore from the Firebase console? I have seen this error before when Firestore is not properly set up from the console.

sr5434 commented 3 months ago

When I create a firebase app instead of using a service account it works sometimes

caravelle-inc commented 3 months ago

@lahirumaramba @sr5434 I am also experiencing this issue.

I have two projects, one for development and one for production, and I am initializing Firestore using a service account. The development project does not have this issue. However, when I set up the production project yesterday, the problem occurred. (BTW, the same problem occurs in the production project, even in the emulator environment.) We have confirmed that the correct service account for the project is being loaded.

Here are the details:

// package.json
{
 "engines": {
    "node": "18"
  },
  "dependencies": {
    "firebase-admin": "^11.11.1",
    "firebase-functions": "^5.0.1"
  }
}
// firebaseConfig.js
const admin = require("firebase-admin");
require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` });
const serviceAccount = require(process.env.SERVICE_ACCOUNT_KEY);

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});
console.log(serviceAccount);

const db = admin.firestore();
module.exports = { db };

I found the same information and wanted to share it for your reference: https://ja.stackoverflow.com/questions/99288/node-js-%e3%81%a7-gcp-%e3%81%ae-cloud-functions-%e3%81%8b%e3%82%89-firestore-%e3%81% ae%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95%e3%81%8c%e5%88%86%e3%81%8b%e3%82%89%e 3%81%aa%e3%81%84

Thank you for reading!

caravelle-inc commented 3 months ago

Additionally, I conducted further investigations and would like to report the findings for your reference.

Firstly, regarding the Firestore activation, I was able to manually add data from the console screen, so I believe Firestore has been successfully activated.

Next, I tried the recommended method from the official documentation as follows, but the error situation remains the same.

const { getFirestore } = require("firebase-admin/firestore");
const { initializeApp } = require("firebase-admin/app");

const app = initializeApp();
console.log("initializeApp", app);

const db = getFirestore();
console.log("getFirestore", db);

module.exports = { db };
lahirumaramba commented 3 months ago

I have two projects, one for development and one for production, and I am initializing Firestore using a service account. The development project does not have this issue. However, when I set up the production project yesterday, the problem occurred.

Did you follow the same steps to generate both service accounts and were they generated on the Firebase Console?

caravelle-inc commented 3 months ago

Yes, I believe I followed the same steps. I created both of these projects at the same time about a month ago. I issued the service account key for the production project a few days ago.

dconeybe commented 3 months ago

I'll take a look at this tomorrow. For now, do you have some log output showing the entire stack trace and error message that you could post?

sr5434 commented 3 months ago

I was able to get the code working when I used the Firebase NPM package on the backend and created a web app in the firebase console.

dconeybe commented 3 months ago

Is your problem solved?

sr5434 commented 3 months ago

Yes

caravelle-inc commented 3 months ago

The production project required a specified database ID. The issue is resolved. Thank you!

edsonboldrini commented 2 months ago

Hello @sr5434 and @caravelle-inc

I was able to get the code working when I used the Firebase NPM package on the backend and created a web app in the firebase console.

The production project required a specified database ID. The issue is resolved. Thank you!

I'm stuck with the same problem. Can you provide the full steps of what solved your problem? if it isn't a bother I will appreciate to see your package.json, firebaseConfig.js/ts and also the calling for the collection, for example: a simple get() to get all documents of a collection.

tushgup commented 2 months ago

I'm also facing the same problem, could someone share what they did to resolve it?

Fadi0950 commented 2 months ago

currently i am using debian 12 Here's the workflow:

I created a project in google cloud platform. I went to Firebase, created a Firebase project, and then set up Cloud Fire store. I navigated to the google cloud platform service account and selected fire base-admin@xyz since it was the only available option. I created a json file key for this service account. In Node-RED, I used the Firestore out node and entered the credentials in the admin config. However, I'm encountering an "Error 5: Not Found" message, and I don't understand why it's not working. the Firebase project is base on the project id of google cloud platform i had created earlier

i want to connect my node-red firestore out to firestore , and add some data to my collection. and i created my database on production mode.

tobyL05 commented 2 months ago

I'm getting the exact same error whenever I try to read/write to cloud firestore. Even after following the docs exactly.

dconeybe commented 2 months ago

For anyone experiencing this issue, please open a new issue with the specifics of your scenario.

ugursirmen commented 1 month ago

@dconeybe Hello, I experienced this issue nowadays and I realized that I deleted '(default)' named database. If you have not this default, you cannot access another one, I know this is stupid but when I re-created a database with '(default)' name and use it, the error resolved :) I hope this was helpful.

dconeybe commented 1 month ago

Thanks for the info, @ugursirmen. We'll look into the effects of deleting the default database.

tobyL05 commented 1 month ago

I managed to solve this issue by adding my database id to getFirestore() like so getFirestore(<database_id>). I didn't realize/forgot that I set my own custom database id in the Firebase Console when I created my database nor did I realize that the database id was there on the console the whole time.

Screenshot 2024-07-15 at 22 37 28

dconeybe commented 1 month ago

Note: I've opened a ticket to get the error message fixed to include the name of the database that was "not found" (Googlers can see details in b/354018499).

Dafomchollom commented 1 month ago

I managed to solve this issue by adding my database id to getFirestore() like so getFirestore(<database_id>). I didn't realize/forgot that I set my own custom database id in the Firebase Console when I created my database nor did I realize that the database id was there on the console the whole time.

Screenshot 2024-07-15 at 22 37 28

this works fine for me thanks

maanmuca commented 1 month ago

firebaseConfig

thanks , it worked only in that way

every821 commented 4 weeks ago

thanks