Home > Blockchain >  Firebase Cloud Functions: Error while deploying function
Firebase Cloud Functions: Error while deploying function

Time:08-19

I'm deploying the following function to firebase:

const functions = require("firebase-functions");
const firebase = require("firebase-admin");
firebase.initializeApp()
var firestore = firebase.firestore()

exports.resetCreditsForFreeUsers = functions.pubsub
    .schedule('* * * * *')
    .onRun(async (context) => {
        const users = firestore.collection('tests')
        const user = await users.get()
        user.forEach(snapshot => {
            snapshot.ref.update({ balance: 10 })
        })
        return null;
    })

with the command firebase deploy --only functions and the output is:


i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

> lint
> eslint

   functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
   functions: required API cloudbuild.googleapis.com is enabled
   artifactregistry: required API artifactregistry.googleapis.com is enabled

Error: Failed to make request to https://serviceusage.googleapis.com/v1/projects/*PROJECTNAME*/services/runtimeconfig.googleapis.com

I can't figure out how to solve this error.

EDIT WITH Dharmaraj answer I tried your answer with Promise.

Error:

   functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
   artifactregistry: required API artifactregistry.googleapis.com is enabled
   functions: required API cloudfunctions.googleapis.com is enabled
   functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing codebase default for deployment
i  functions: preparing functions directory for uploading...
i  functions: packaged C:\Users\andre\Documents\firebase-functions\functions (78.21 KB) for uploading

Error: Failed to make request to https://cloudfunctions.googleapis.com/v2alpha/projects/betty-sport/locations/-/functions 

EDIT changing update() with set()

Error: Failed to list functions for betty-sport

CodePudding user response:

The .update() returns a Promise so you should wait for it to resolve. Try refactoring the code as shown below:

exports.resetCreditsForFreeUsers = functions.pubsub
  .schedule('* * * * *')
  .onRun(async (context) => {
    const users = firestore.collection('tests')
    const user = await users.get()

    const promises = user.docs.map(d => d.ref.update({ balance: 10 }));

    await Promise.all(promises);

    return null;
  })

Alternatively, you can also use a batched write to update up to 500 documents at once instead of individually updating them.

  • Related