11

I am making a "scheduled" firebase-function that gets data from external API source and save it at firestore.

const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { default: Axios } = require("axios"); admin.initializeApp(); exports.getIndexData = functions.pubsub.schedule('every 5 minutes').onRun(async() => { try { const response = await Axios( "https://financialmodelingprep.com/api/v3/quotes/index?apikey=myAPIKEY" ); const data = response.data; const writeResult = await admin .firestore() .collection("index") .doc("sorted-index") .set({ indexData: data,timeStamp:Date.now()}); } catch (error) { console.log(error); } return null; }); 

this is my firebase-function code. and it works totally fine when I run the function separately, and also I tested the function with "google cloud platform cloud function test". Data is successfully set it at firestore when I run a function seperately.

However, it doesn't work when I deploy the function, and I think it is about scheduled-function stuff

{ "insertId": "184t0npf9hhej7", "jsonPayload": { "pubsubTopic": "projects/my-project/topics/firebase-schedule-getIndexData-us-central1", "targetType": "PUB_SUB", "status": "UNAUTHENTICATED", "jobName": "projects/my-project/locations/us-central1/jobs/firebase-schedule-getIndexData-us-central1", "@type": "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished" }, "resource": { "type": "cloud_scheduler_job", "labels": { "job_id": "firebase-schedule-getIndexData-us-central1", "project_id": "my-project", "location": "us-central1" } }, "timestamp": "2020-12-09T08:48:01.142830977Z", "severity": "ERROR", "logName": "projects/my-project/logs/cloudscheduler.googleapis.com%2Fexecutions", "receiveTimestamp": "2020-12-09T08:48:01.142830977Z" } 

So I was keep searching for this UNAUTHENTICATED error, and people says I hvae to change some permission options. so I gave allUsers and allAuthenticated Users a Cloud Functions Invoker permission. still not working.

Any Idea or solution on this? Thank you.

10
  • 1
    I don't know if this is the cause of your problem but you should only do return null; when all the asynchronous work is completed. Concretely you should move the return null; just after const writeResult = await admin... in the try(). and you should also add it after the console.log() in the catch(). Commented Dec 9, 2020 at 12:16
  • Can you correctly view the topic and the function as it is indicated (are the logs in the cloud scheduler more clear about the issue?) firebase.google.com/docs/functions/… Commented Dec 10, 2020 at 14:28
  • @Juancki I somehow achieved same result by using function.https thing and set google scheduler to get the url every 5mins instead of pubsub. But still not working with pubsub thing.. pubsub keep sending this UNAUTHENTICATED error code. I am keep digging it. Commented Dec 11, 2020 at 0:03
  • @RenaudTarnec I will try. the function worked fine with the test in google cloud. Commented Dec 11, 2020 at 0:05
  • 3
    It got fixed automatically for me today without doing anything Commented Dec 15, 2020 at 3:33

2 Answers 2

6

Try these steps

Disable and re-enable Cloud Scheduler API.

If you are still getting the issue, try this and repeat the above step.

Sign up to request clarification or add additional context in comments.

2 Comments

Disabling and enabling the Cloud Scheduler API worked well for me! Thanks a lot
This worked for me also. I disabled and then redeployed all cloud functions for good measure. Thanks
4

You forgot to include "context" in your arguments. https://firebase.google.com/docs/functions/schedule-functions

.onRun(async (context) => { ... }) 

1 Comment

I was very skeptical of this answer since I was not using any arguments, and Javascript does not impose any argument restrictions.. But it somehow fixed it for me? I am using typescript, and exporting the method from another file like this export async function myFunction(context: EventContext) { }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.