Firebase导入服务引发错误 [英] firebase import service throws error

查看:124
本文介绍了Firebase导入服务引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase函数,并且我想使用服务帐户密钥json将App初始化为凭证,但出现错误

I'm using firebase functions and I want to initializeApp with service account key json into credential and I get the error

'{"type"类型的参数:字符串; "project_id":字符串; "private_key_id":字符串; "private_key":字符串; "client _..."不可分配给字符串| ServiceAccount"类型的参数. 类型'{"type":字符串; "project_id":字符串; "private_key_id":字符串; "private_key":字符串; "client _..."与"ServiceAccount"类型没有共同的属性.

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'.

我的index.ts文件

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

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"


}

这行代码中的错误

        credential: admin.credential.cert(serviceAccount),

推荐答案

我也经历过这种情况.解决方案是将类型ServiceAccount并强制转换为该类型,该类型是从该json文件导入的整个对象.

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

这篇关于Firebase导入服务引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆