node.js - firebase 导入服务抛出错误

标签 node.js firebase google-cloud-functions

我正在使用 firebase 函数,我想使用服务帐户 key json 将应用程序初始化为凭据,但出现错误

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 文件

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

服务账户.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 文件导入的整个对象转换为该类型。

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

关于node.js - firebase 导入服务抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272165/

相关文章:

javascript - 数据表不适用于 Firebase

firebase - Flutter:通过 FlutterFire 发送电子邮件验证码?

google-cloud-platform - GCP 数据流与云函数

google-cloud-platform - 将 Google Cloud Functions 的访问权限限制在给定的网络?

node.js - time_wait 过多的 Node 应用程序

javascript - Aurelia 应用程序 "Hello World"根本不工作

node.js - 如何使用 headless : true? 使用 puppeteer 下载文件

Node.js 防止使用已吊销的证书进行连接

ios - 使用 Firebase Auth 时应用崩溃,原因 : 'Default app has already been configured.'

typescript - 如何使用Firebase模拟器pubsub在本地测试定时功能?