Skip to content

Latest commit

 

History

History
96 lines (70 loc) · 1.89 KB

best-practices.md

File metadata and controls

96 lines (70 loc) · 1.89 KB

Firebase / Best practices

Deployed functions should return Promise when performing asynchronous tasks

GOOD

exports.signup = functions.https.onRequest((req, res) => {
    return admin.database()
        .ref("/users/" + req.query.id)
        .once("value")
        .then(() => {
            // ...
        });
});

BAD

exports.signup = functions.https.onRequest((req, res) => {
    admin.database()
        .ref("/users/" + req.query.id)
        .once("value")
        .then(() => {
            // ...
        });

    // Firebase console => Function returned undefined, expected Promise or value.
});

Define RESTful API routes

See also: Designing Web APIs using Google Firebase Functions: Achieving True Routing

GOOD

const app = require("express")();

// ...

app.get("/users/:uid", function(req, res) => {
    // ...
});

exports.api = functions.https.onRequest(app);

// => /api/users/npWxk05ZCKMcYb0OaDSJffYQZZq1

BAD

exports.getUser = functions.https.onRequest((req, res) => {
    const uid = req.query.uid;

    // ...
});

// => /getUser?uid=npWxk05ZCKMcYb0OaDSJffYQZZq1

Let promises get rejected when handling errors unless there is a fallback strategy

GOOD

exports.signup = functions.auth.user().onCreate(user =>
  Model.create(user)
    .then(() => Service.createCustomer(user))
    .catch(err => {
      console.error(JSON.stringify(err));

      // Re-throw the error to fail the Promise explicitly.
      throw err;
    })
);

// => Function execution took 60 ms, finished with status: 'error'

BAD

exports.signup = functions.auth.user().onCreate(user =>
  Model.create(user)
    .then(() => Service.createCustomer(user))
    .catch(err => {
      console.error(JSON.stringify(err));
    })
);

// => Function execution took 60 ms, finished with status: 'ok'