Firebase导入服务引发错误

编程入门 行业动态 更新时间:2024-10-27 22:23:45
本文介绍了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", "client_id": "xxxxx", "auth_uri": "accounts.google/o/xxxxx", "token_uri": "accounts.google/o/oxxxxn", "auth_provider_x509_cert_url": "www.googleapis/oauth2/v1/certs", "clixxxxxx": "www.googleapis/robot/v1/metadataxxxxirebase-adminsdk-uxxxxxxrviceaccount" }

这行代码中的错误

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导入服务引发错误

本文发布于:2023-10-24 09:18:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!