Unable to deploy firebase cloud function - javascript

I am trying to deploy a firebase cloud function. It works on local firebase emulator but when I try to deploy, it's giving the following error on firebase console cloud functions log:
{"#type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":13,"message":"Failed to configure trigger for event-type:providers/cloud.firestore/eventTypes/document.update resource:projects/kapsule-1099e/databases/(default)/documents/roles/warehouser/{warehouseName}/{warehouseName}/Inventory/{assetName} service:firestore.googleapis.com."},"authenticationInfo":{"principalEmail":"malickfaizann#gmail.com"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/kapsule-1099e/locations/us-central1/functions/sendNotificationOnLowStock"}

I solved it. Path params cannot have same name even if their values are same. I renamed one of them and it worked.
Final path is:
roles/warehouser/{warehouseName}/{warehouseName2}/Inventory/{assetName}

Related

Firebase secrets not defined in process.env

I'm writing a Firebase function with Cloud Storage trigger. Like this
const functions = require('firebase-functions')
const doSomethingWithSecrets = require('./doSomethingWithSecrets')
const doSomethingWhenUploaded = functions.runWith({
secrets: ["MY_SECRET_1", "MY_SECRET_2", "MY_SECRET_3"]
}).storage.object().onFinalize(o => {
functions.logger.debug([
process.env.MY_SECRET_1 // undefined
process.env.MY_SECRET_2 // undefined
process.env.MY_SECRET_3 // undefined
])
doSomethingWithSecrets(process.env.MY_SECRET_1, process.env.MY_SECRET_2, process.env.MY_SECRET_3)
// Error: Invalid secret.
})
All three of them returns undefined. I've made sure that they're properly set. They show up both when using firebase functions:secret:accesss MY_SECRET_1 and in Google Cloud Console.
What's wrong?
Additional info
I previously used it with only one secret and it worked. I don't know what happened, I'm using nvm and lost track of which Node version I used when it worked, so it may be a clue.
process.env returns all the env like normal and none of my secrets shows up.
Update your firebase-tools and the issue will resolve itself. I was dealing with this issue all day today and found a git hub issue that fixed the problem in the latest release of 10.9.2.
npm install -g firebase-tools
https://github.com/firebase/firebase-tools/issues/4540
https://github.com/firebase/firebase-tools/issues/4459
I encountered the exact same issue. After much suffering I finally got it to work. When you start using secrets in your functions, you need to redeploy all functions and NOT just the individual function.
e.g
firebase deploy --only functions:myFunc
results in my secret values coming through as 'undefined' when accessed from my function using runWith(), however after doing a full functions deploy
firebase deploy --only functions
everything worked.

MERN: 'Invalid API Key' Heroku Deployment?

How do I get Firebase Auth to work on Heroku Deployment?
Auth works in MERN app's local development without any issues. All private information is stored in an .env file and then called in my react app with process.env.VARIABLE_NAME.
However, when I deploy to Heroku production and add the env files as shown here, I get the error: "Your API key is invalid, please check you have copied it correctly"
This is my deployed Heroku app: https://evening-fortress-01391.herokuapp.com/login
REACT_APP_FIREBASE_API_KEY=XXXXXXXX
REACT_APP_FIREBASE_AUTH_DOMAIN=XXXXXXXX
REACT_APP_FIREBASE_PROJECT_ID=XXXXXXXX
REACT_APP_FIREBASE_STORAGE_BUCKET=XXXXXXXX
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=XXXXXXXX
REACT_APP_FIREBASE_APP_ID=XXXXXXXX
Also, running the command heroku config displays the inputted env variables in the terminal.
Any idea why Auth works locally but not in deployment? Thank you!
Answer because too long for a comment.
Sounds super hard to debug - also your app just shows a login and not the API key error =) To me it sounds like either just like it says, an incorrect API key or a formatting issue.
I would make a test config variable using the same characters as your API key like REACT_APP_FIREBASE_POTATO=p07470-test-example-foooobar and then in your frontend app just console.log("TEST" + process.env.REACT_APP_FIREBASE_POTATO);. Then publish that and check it in production.
This tests are the environment variables working at all and are there some character encoding issues (do you see the exact characters in the console as you should)

Prism V2 access denied to postgres database

I have created a brand new nodejs project with Prism2 and Postgres 13
Postgres is on a windows VM in the same network. Im testing Prism for the first time and cannot get it to deploy the database schema at all. I cant find anything that tells me how to fix this problem.
When i run npx prisma migrate dev all i get is Error: P1010: User 'johndoe' was denied access on the database 'db.public'. Ive searched for this error message and cant find any resolution, what is the issue here?
Actually the problem was resolved with some changes to the Postgres config files. By default its only listening to the local IP address of the machine that its installed on. With help from this post i was able to resolve the issue.
Made sure that the postgresql.conf listen_addresses was set to '*'. This was correct by default.
Made sure the pg_hba.conf IPv4 host was set within the range of my network. This was changed from host all all 127.0.0.1/32 to host all all 192.168.1.1/24
Restart the postgres service and now everything works as expected.
It's a bug in prisma library itself, has been reported and acknowledged. You can checkout the exact issue here

How to use firestore emulator from client

I want to test locally my firebase functions.
These functions make firestore queries.
So i start the emulator firebase emulators:start and in my client i use firebase.functions().useFunctionsEmulator('http://localhost:5001').
My functions work well when i call them in my client. I can read/write data inside the firestore emulator.
The problem :
I want to read the firestore emulator data directly inside my client, like :
firebase.firestore().collection('tests').get().then(tests => {
console.log( tests.docs.map(test=>test.map) )
})
but i can't find how to set the firestore emulator inside my client.
here what i tried:
1) Firestore setting
firebase.firestore().settings({
host:'http://localhost:8080',
ssl:false
})
result :
i get #firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. inside my client console.
The http request returns 'Not found'
2) Set the emulator url inside my firebaseConfig
var firebaseConfig = {
// ...
databaseURL: "http://localhost:8080",
// ...
}
firebase.initializeApp(firebaseConfig)
in this case, the remote server (https://firestore.googleapis.com..) is requested.
So i want to setup one of these two cases :
1) Using the remote firestore inside my functions emulators
OR
2) Using the local firestore emulator inside my client code.
Anyone has already done this ?
Install the testing lib
npm i -D #firebase/testing
Setup and start the emulator in another terminal:
firebase setup:emulators:firestore
firebase serve --only firestore
Setup the tests
const firebase = require("#firebase/testing");
// Helper function to setup test db
function authedApp(auth) {
return firebase
.initializeTestApp({ projectId: FIRESTORE_PROJECT_ID, auth })
.firestore();
}
// Setup methods
beforeEach(async () => {
// Clear the database between tests
await firebase.clearFirestoreData({ projectId: FIRESTORE_PROJECT_ID });
});
// Clean up apps between tests.
afterEach(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
});
Run the tests
it("should retrieve correct item", async () => {
// Init test db
const db = authedApp(null);
// Manually add item to collection
const ref = await db.collection(COLLECTION_NAME).add({name: 'test item'});
// Fetch item by id
const resp = await db.collection(COLLECTION_NAME).doc(ref.id).get();
// test the output
expect(resp).toBeDefined();
expect(resp).toEqual(expect.objectContaining({name: 'test item'}));
});
Of course your particular setup and circumstances will differ, but this at least should give you a general idea. More info: https://firebase.google.com/docs/rules/unit-tests
Note from 'Test your Cloud Firestore Security Rules'
Data written to the Cloud Firestore emulator is held in memory until
the emulator is stopped. If the emulator is run continuously, this may
have an impact on test isolation. To ensure that data written in one
test is not read in another, either explicitly clear your data with
clearFirestoreData, or assign a different project ID for each
independent test: when you call firebase.initializeAdminApp or
firebase.initializeTestApp, append a user ID, timestamp, or random
integer to the projectID.
Edit: I wrote a blog post a while back, which goes into more detail about the subject.
I had the same problem too. I found the following example, which looks like its still work in progress:
https://github.com/firebase/quickstart-nodejs/tree/master/firestore-emulator/browser-quickstart
They didn't use #firebase/testing directly in their example. When I did try to embed #firebase/testing in my webpack code, it tries to connect via grpc, which attempts to do fs.existsSync, which doesn't exist in webpack browser context. They prefer to enable the functionality via WebChannel instead.
Some caveats as of Nov 2019:
You might see errors in the console connecting to localhost:8080 with the X-Goog-API. I am not sure what is that for.
You might get the following error: #firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
Despite having those errors, my functions were still able to connect to the local firestore.
Versions I had at time I was testing:
firebase-js-sdk 6.3.1
firebase-tools 7.3.1
Update 11/18:
I raised an issue within quickstart-nodejs github and it seems I just needed to use the latest versions of everything.
Versions:
firebase-js-sdk v7.4.0
firebase-tools v7.8.0
Ok i found how to do it :
1) launch the functions emulator locally:
set GOOGLE_APPLICATION_CREDENTIALS=./privatekey.json && firebase serve --only functions
2) then client side:
if (process.env.NODE_ENV === 'development') {
firebase.functions().useFunctionsEmulator('http://localhost:5001')
}
Okay, this is trivial... In your firestore cient config you should have provided the host, not the origin for firestore (the protocol is set using the ssl parameter):
firebase.firestore().settings({
host: 'localhost:8080',
ssl: false
})
At least this solved it for me when I had the exact same error.
Just FYI to anyone who's reading this - if you run into problems with the firestore client, you can use debug level logging, just set firebase.firestore.setLogLevel('debug'). Had the OP done that, he might have noticed that firebase is accessing firestore at http://http://localhost:8080/...
Define the FIRESTORE_EMULATOR_HOST environment variable
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
export FIRESTORE_EMULATOR_HOST=localhost:8080
or just add FIRESTORE_EMULATOR_HOST=localhost:8080 to your .env file

Firebase Deploy - "Unable to Authorize" error

I tried to deploy a very simple site for a Firebase project I have set up, and followed the following steps:
firebase login (I was already logged in since I have other projects I'm working on)
firebase init (created the .firebaserc and database.rules.json files).
firebase deploy
On deploy, I keep getting the following error message:
λ firebase deploy
! Your CLI authentication needs to be updated to take advantage of
new features.
! Please run firebase login --reauth
Error: Unable to authorize access to project [PROJECT NAME]
I succesfully ran firebase login --reauth and I still get this.
Anyone know what I could be doing wrong here?
First warning shows you have to update firebase-cli.
Run this command to update:
npm install -g firebase-tools
Your project may not have been linked which causes that error.
Run this command to link the project:
firebase use --add
It will list down the projects in your firebase account, select the correct one and try deploying the project using:
firebase deploy
PS: You don't have to append project_id to deploy command every time once you run use --add command
Found my answer in another post
It seems that when deploying, you're supposed to deploy with:
firebase deploy --project [PROJECT ID, not PROJECT NAME]

Categories