Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot deploy Firestore Cloud Function

I'm trying to deploy a simple function on Firebase Cloud Functions but console logs an error that I can't figure out where is

My index.js:

const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp()

exports.updateAccount = functions.firestore.document('accounts/{client_id}/movements').onWrite(change => {
    const document = change.after.exists ? change.after.data() : null
    console.log(document)
})

Console says:

⚠  functions: failed to create function updateAccount
HTTP Error: 400, The request has errors


Functions deploy had errors with the following functions:
        updateAccount


To try redeploying those functions, run:
    firebase deploy --only functions:updateAccount


To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.
like image 553
Marcelo J Forclaz Avatar asked Jan 22 '19 13:01

Marcelo J Forclaz


People also ask

Why does cloud deployment fail?

Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. Your source code must contain an entry point function that has been correctly specified in your deployment, either via Cloud console or Cloud SDK.

Does Firebase Functions support Node 16?

The engines field is required; it must specify one of the supported Node. js versions in order for you to deploy and run functions. Currently firebase init functions sets this field to 16 .


1 Answers

Your cloud function argument always points to the document level, not the collection level.

exports.updateAccount = functions.firestore
  .document('accounts/{client_id}/movements/{movement_id}')
  .onWrite(change => {
     const document = change.after.exists ? change.after.data() : null
     console.log(document)
})

You are trying to make the deployment in the collection level, not in the document level.

like image 77
pepe Avatar answered Oct 14 '22 03:10

pepe