Я новичок в облачных функциях. Я следовал за документами и новым синтаксисом инициализации для firebase-admin, но получаю ошибку аутентификации при запуске функции на сервере и на локальном хосте.
https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase-admin
@firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND\"."}
Я попытался создать sdk с serviceAccountKey
, та же проблема. https://firebase.google.com/docs/admin/setup#initialize_the_sdk
Функция запускается на локальном хосте и на сервере, и выдается ошибка, когда выполняется вызов базы данных в gettingUser()
При локальном запуске после сборки:
$ firebase experimental:functions:shell
firebase > const data = {some_article...}
firebase > addingNewArticle(data)
'Successfully invoked function.'
firebase > > New article created articleId1
DEBUG: @firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND\"."}
/index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
export const addingNewArticle = functions.database
.ref('/articles/{articleId}')
.onCreate(async (snapshot, context) => {
const burgerId = context.params.burgerId
const result = await gettingUser();
console.log(`From onCreate ${result}`);
console.log(`New article created ${articleId}`)
const burgersVal = snapshot.val()
const name = burgersVal.name
const addedById = burgersVal.addedById
console.log(`${name} was added by: ${addedById}`);
console.log(`burgersVal: ${burgersVal}`);
console.log(`snapshot: ${snapshot}`);
return Promise.all([])
})
export async function gettingUser() {
const result = await admin.database().ref(`users/fnkjasdfadsfna.d`).once('value');
console.log(`Looging new user ${result.val()}`);
return result.val()
}