Trying to connect with any of the methods described in: https://firebase.google.com/docs/admin/setup#initialize_the_sdk
results in this error:
FAILED_PRECONDITION: The Cloud Firestore API is not available for Cloud Datastore projects
Although trying to connect through the method mentioned here connects successfully: https://firebase.google.com/docs/firestore/quickstart
I realized this only when I found this SO question:
https://stackoverflow.com/questions/57422882/firebase-error-9-failed-precondition-the-cloud-firestore-api-is-not-availabl
I think the error is misleading and a better error or explanation would clarify what's the problem
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
The error message looks on point to me. In this case the SDK attempted to connect to a project where Firestore was not enabled (because it had Cloud Datastore already enabled). The error message clearly states that. Also note that this is an error produced by the Firestore backend and not the SDK. So there's not much we can do about it in this codebase.
The way to resolve this sort of errors involves figuring out why the SDK connected to the incorrect project in the first place. And it's almost always due to incorrect initialization. The easiest way to initialize the SDK is as follows:
const admin = require('firebase-admin');
admin.initializeApp();
This will work in Cloud Functions and all other managed GCP environments. It will also work locally as long as you set the GOOGLE_APPLICATION_CREDENTIALS environment variable.
Here's the definite guide to getting started with the Admin SDK: https://firebase.google.com/docs/admin/setup
@hiranya911
The error isn't because the Firestore was not enabled, because I had it enabled on my project. It was for some reason not reading the serviceAccount.json file.
So the snipped you actually wrote doesn't work and throws the same error reported here even with the correct environment variable in place. The only snipped that worked is specifying admin.credential.cert(serviceAccount) like in this code snippet:
import * as admin from 'firebase-admin';
const serviceAccount = require('../serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
Snippet works fine (tested on multiple environments). You just need to set the environment variable correctly. Otherwise, the SDK will connect to an incorrect project (I believe it connects to some default project used by the gcloud command line tools), where Firestore is not enabled.
Oh I see, maybe that's what's happening, the default project in gcloud command line tools is indeed different from this project that I was trying to run Firestore for.
Thanks for clarifying
Most helpful comment
@hiranya911
The error isn't because the Firestore was not enabled, because I had it enabled on my project. It was for some reason not reading the serviceAccount.json file.
So the snipped you actually wrote doesn't work and throws the same error reported here even with the correct environment variable in place. The only snipped that worked is specifying
admin.credential.cert(serviceAccount)like in this code snippet: