I'm using firebase functions and I want to initializeApp with service account key json into credential and I get the error
Argument of type '{ "type": string; "project_id": string; "private_key_id": string; "private_key": string; "client_...' is not assignable to parameter of type 'string | ServiceAccount'. Type '{ "type": string; "project_id": string; "private_key_id": string; "private_key": string; "client_...' has no properties in common with type 'ServiceAccount'.
my index.ts file
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import {serviceAccount} from './serviceAccount'
console.log(functions.config())
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL:functions.config().firebase
});
export const firestore = admin.firestore();
export const firebase = admin.database();
serviceAccount.ts
export const serviceAccount = {
"type": "service_account",
"project_id": "lxxxxxx",
"private_key_id": "xxxxxx",
"private_key": "-----BEGIN PRIVATE KEY-----xxxxxxx---END PRIVATE KEY-----\n",
"client_email": "firebase-axxxxx-9b58b.iaxxxceaccount.com",
"client_id": "xxxxx",
"auth_uri": "https://accounts.google.com/o/xxxxx",
"token_uri": "https://accounts.google.com/o/oxxxxn",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"clixxxxxx": "https://www.googleapis.com/robot/v1/metadataxxxxirebase-adminsdk-uxxxxxxrviceaccount.com"
}
the error in this line of code
credential: admin.credential.cert(serviceAccount),
I've experienced this too. The solution is to bring the type ServiceAccount
and cast to that type the whole object imported from that json file.
import firebase from 'firebase'
import * as firebaseAdmin from 'firebase-admin'
import firebaseConfig from '../firebaseConfig.json'
import firebaseAccountCredentials from '../serviceAccountCredentials.json'
const serviceAccount = firebaseAccountCredentials as admin.ServiceAccount
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: firebaseConfig.databaseURL
});
I'm not very experienced with TypeScript, but I think this will work as a temporary solution until someone proposes a better one.
let regularObj = {};
Object.assign(regularObj, serviceAccount);
admin.initializeApp({
credential: admin.credential.cert(regularObj),
databaseURL: functions.config().firebase
});
I've had an issue like this.
Try converting to JSON and back.
Like this:
admin.initializeApp({
credential: admin.credential.cert(JSON.parse(JSON.stringify(serviceAccount))),
databaseURL: functions.config().firebase
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With