All of the code below works after firebase deploy
but throws error when run on local machine:
Error: 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".
How do I change the following config so that it works in local test environment and in Firebase after deploy?
foo.js works locally and on firebase
getUsersFunction works on firebase but not locally with firebase emulators:start --only=functions
I set up two firebase endpoints and run them locally with:
firebase emulators:start --only=functions
index.js
"use strict";
const fooFunction = require("./foo");
const getUsersFunction = require("./getUsers");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.fooFunction = functions.https.onRequest((req, res) => {
fooFunction.handler(req, res);
});
exports.getUsersFunction = functions.https.onRequest((req, res) => {
getUsersFunction.handler(req, res, admin);
});
foo.js
exports.handler = function (req, res, database) {
res.send("foo ran successfully");
};
getUsersFunction.js
exports.handler = function (ref, res, admin) {
admin
.auth()
.listUsers(1000)
.then(function (listUsersResult) {
console.log(listUsersResult);
res.send("done");
return null;
})
.catch(function (error) {
console.log("Error listing users:", error);
});
};