0

I created a Firebase Cloud Function that will send someone an email after they fill out a form. It uses the onCreate event type so that whenever a user hit's the submit button, it will take their email they put in the form and send them an email with certain details they put in the form.

The Function works as expected and I can get some of the field values from the document that was created into the email using snapshot (snap.data().email). The issue is that one of the fields in the document is a reference to another document in a different collection. When I try to return one of the field values from the doucment reference (snap.data().month) it returns as undefined (which I expect to happen).

My question is how do I retrieve the data from the document that is being referenced in one of the fields in the Bookings document?

Edit: Just to clarify, I have a collection called Bookings, which has a field called bookingTime. The bookingTime field is of type reference, and it references another document in a collection called BookingTime. So the bookingTime field looks something like bookingTime: BookingTime/fsififeSo032

Edit 2: So I have seen [this] (What is Firebase Firestore 'Reference' data type good for?) StackOverflow post. It does provide some clarification on Reference types. That being said, Firebase is very new to me and I am not sure how to incorporate this example into a Cloud Function. If anyone could link an example or tutorial (I have tried looking for one for a while now) that implements something similar to what I am trying to do, that would be much appreciated.


var transporter = nodemailer.createTransport({

    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: 'email_placeholder',
        refreshToken: 'refreshToken_placeholder',
        accessToken: 'accessToken_placeholder',
        clientId: 'clientId_placeholder',
        clientSecret: 'clientSecret_placeholder'
    }
});


exports.apptConfirmEmail = functions.firestore
    .document('Bookings/{BookingsId}')
    .onCreate((snap, context) => {

        const mailOptions = {
            from: 'email_placeholder',
            to: snap.data().email, //email that user put in the form (works as expected)
            subject: 'Booking Confirmation',
            html: `<h1>Booking Confirmation</h1>
                                    <p>
                                        <br>

                                          
                                        <br>
                                       <b>UniqueCode: </b>${snap.data().UniqueCode}<br> //retrieves unique code as expected
                                       <b>Month: </b>${snap.data().month}<br> //returns undefined because the month field is in another document that a field in the Booking document is referencing 
    

                                    </p>`
        };


        return transporter.sendMail(mailOptions, (error, data) => {
            if (error) {
                console.log(error)
                return
            }
            console.log("Sent!")
        });
    });
HeyJoel91
  • 1
  • 1
  • Reference types IMO are pretty useless. There's a decent discussion on the topic here ~ https://stackoverflow.com/q/46568850/283366 – Phil Nov 27 '22 at 23:04
  • Hey Phil, I have heard people mention this before in regards to reference types being not very useful. I am pretty new to Firebase and Cloud Functions and was curious how this could be accomplished without restructuring my Firestore database. I've seen the StackOverflow article you linked but I am not sure how to implement that with a Cloud Function. – HeyJoel91 Nov 27 '22 at 23:28
  • Whether you use a reference type or a string with either the full doc ref path or just an ID, you need to execute a query to return the related data using the admin SDK. Alternately, store your data de-normalised which is something NoSQL databases like Firestore excel at – Phil Nov 27 '22 at 23:31

0 Answers0