firebase import service throws error

二次信任 提交于 2020-02-24 05:10:06

问题


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),

回答1:


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 
});



回答2:


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
});



回答3:


Following code worked for me:

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 
});


来源:https://stackoverflow.com/questions/50272165/firebase-import-service-throws-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!