logo
down
shadow

How to do integration tests for Firebase HTTP functions with Firestore


How to do integration tests for Firebase HTTP functions with Firestore

By : David Iacovino
Date : November 20 2020, 04:01 AM
To fix this issue You have to setup a Firestore emulator locally (assuming you already have firebase-cli installed):
$ firebase setup:emulators:firestore
code :


Share : facebook icon twitter icon
Error: HTTP Error 400, The Request has errors. Firebase Firestore Cloud Functions

Error: HTTP Error 400, The Request has errors. Firebase Firestore Cloud Functions


By : lasu moses
Date : November 05 2020, 03:01 PM
help you fix your problem This was happening to me too, then I realized that at the 2nd level, firestore only allows documents and not collections.
I was attempting to listen to this path:
code :
/collection/document/{wildcard}
/collection/{wildcard}
/collection/document/collection/{wildcard}
I can not write data from the Firebase Cloud Functions in Firebase Firestore

I can not write data from the Firebase Cloud Functions in Firebase Firestore


By : Mike
Date : March 29 2020, 07:55 AM
like below fixes the issue Please see the documentation to understand how Cloud Functions works with async code.
You're not returning a promise from your function. If your function performs any async work that yields a promise, you have to indicate to Cloud Functions wait for that work to complete by returning a promise that resolves when the work is complete:
code :
exports.userCreated = functions.auth.user().onCreate(event => {
    let userId = event.data.uid;
    let userName = event.data.displayName;

    return admin.firestore()
    .collection("users")
    .doc(userId)
    .set({
        name: userName,
        city: "",
        rating: 0
    });
});
Performance difference Firestore through Firebase Functions vs Firestore SDK

Performance difference Firestore through Firebase Functions vs Firestore SDK


By : sandeep
Date : March 29 2020, 07:55 AM
it helps some times Yes but No depending on your use case.
If you have small amount of users with relatively low usage (in terms of the given quota), it is recommended to apply Cloud Functions. As stated in the documentation, Firebase Cloud Function offers big quota in terms of Resource limits, Time limits and Rate limits with good pricing especially for the Spark plan (FREE).
Combining Firebase Firestore query results in Firestore Functions before sending email

Combining Firebase Firestore query results in Firestore Functions before sending email


By : Thanhson Vu
Date : March 29 2020, 07:55 AM
should help you out Ok, so here's what I was able to come up with. I fired this up in a debugger to step through and make sure everything works.
code :
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors');

// Needed this to connect to Firestore, my code not yours
admin.initializeApp();
admin.firestore().settings({ timestampsInSnapshots: true });

// Emulate the transport.sendMail() for debug purposes
let transport = {
    sendMail: (options) => {
        return new Promise((resolve, reject) => {
            console.log(`Sending Mail: ${options}`);
            resolve(options);
        });
    }
}

module.exports.sendMail = functions.https.onRequest((req, res) => {

    if (req.method !== 'POST') // won't have a body
        return res.status(400).send(`Error: ${req.method} is not Accepted.`);

    // extract params from body
    const { name, email, doc, state, city, phone, msg, } = req.body

    let dbPromises = [];
    let firestore = admin.firestore(); // alias to lineup with OP's code

    // extract headers
    const ip1 = req.headers["fastly-client-ip"];
    const ip2 = req.headers["x-forwarded-for"];

    // validate input, if bad: emit Client error
    if (!ip1 || !ip2 || !email)
        return res.status(400).send("Error: Invalid Request.");

    // query previous message existence
    dbPromises.push(firestore.collection('messages').where('ip1', '==', ip1).get());
    dbPromises.push(firestore.collection('messages').where('ip2', '==', ip2).get())
    dbPromises.push(firestore.collection('messages').where('email', '==', email).get())
    dbPromises.push(firestore.collection('blocked-emails').where('email', '==', email).get())

    // Need to return a promise so your function doesn't timeout
    return Promise.all(dbPromises)
        .then(resultDocs => {
            if (resultDocs.length !== 4)
                throw new Error("Programmer Error");

            // validate if there is any result > 0, if any, throw error to the user
            if (resultDocs[0] !== null && resultDocs[0].docs.length !== 0)
                throw new Error(`${ip1} already exists`);
            if (resultDocs[1] !== null && resultDocs[1].docs.length !== 0)
                throw new Error(`${ip2} already exists`);
            if (resultDocs[2] !== null && resultDocs[2].docs.length !== 0)
                throw new Error(`${email} already exists`);
            if (resultDocs[3] !== null && resultDocs[3].docs.length !== 0)
                throw new Error(`${email} is blocked`);

            return null;
        })
        .then(() => {
            // Build message for mailer
            const mailOptions = {
                from: `"${name}" <${email}>`,
                to: 'a_email@gmail.com',
                replyTo: `"${name}" <${email}>`,
                subject: `Contact - ${name}`,
                html: `<div>${name}</div>
                     <div>${email}</div>
                     <div>${doc}</div>
                     <div>${state}</div>
                     <div>${city}</div>
                     <div>${phone}</div>
                     <div>${msg}</div>`,
            }

            let innerPromise = null;

            // Fix headers for cross-origin
            cors()(req, res, () => {
                // send mail returns a promise
                innerPromise = transport.sendMail(mailOptions);
            });

            return innerPromise; // return promise or null
        })
        .then(sendMailResult => {

            if (!sendMailResult) {
                // not sure if transport.sendMail even returns result
                // do validation here if yes
            }

            // write message to store
            return firestore
                .collection('messages')
                .add({
                    name: name,
                    email: email,
                    doc: doc,
                    state: state,
                    city: city,
                    phone: phone,
                    msg: msg,
                    ip1: ip1,
                    ip2: ip2,
                });
        })
        .then(() => {
            return res.status(201).send("Success")
        })
        .catch(err => {
            console.log(err);
            res.status(500).send(String(err));
        })
})
Firebase Firestore extend to Cloud Functions to put more informations by Firebase Authentication

Firebase Firestore extend to Cloud Functions to put more informations by Firebase Authentication


By : user3529034
Date : March 29 2020, 07:55 AM
will help you In the context you are trying to use it, admin.auth is a helper function to get the Auth object of the default application initialized by admin.initializeApp().
Change your line so that the admin.auth() function is called.
code :
admin.auth().getUserByPhoneNumber(/* ... */)
//        ^^
shadow
Privacy Policy - Terms - Contact Us © bighow.org