Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendEmail: Cannot read property 'id' of undefined #15

Open
ghost opened this issue Jun 21, 2018 · 4 comments
Open

SendEmail: Cannot read property 'id' of undefined #15

ghost opened this issue Jun 21, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Jun 21, 2018

After checkout API successfully completes, I can see that the DB triggers call to send email. However this is reporting the following error:

TypeError: Cannot read property 'id' of undefined
at exports.sendEmail.functions.firestore.document.onCreate.event (/user_code/index.js:49:31)
at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:728:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

When I look at the firestore database, I see the 'id' has been added, but it's inside the product's structure which is odd, should it not be at the root of the document?

Another error I get is:

TypeError: event.data.data is not a function
at exports.sendEmail.functions.firestore.document.onCreate.event (/user_code/index.js:50:27)
at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:728:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

@mutebg
Copy link
Owner

mutebg commented Jun 23, 2018

Hi,
Just checked on my demo website, and also didn't work, maybe something changed in firebase functions or firestore since they are still beta and made this half year ago.
Will try to find what courses the problem but that might take me some time.

@ghost
Copy link
Author

ghost commented Jun 23, 2018 via email

@ghost
Copy link
Author

ghost commented Jun 24, 2018

Hello,

This link proved very useful in getting sendEmail () to work: https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore

Essentially I had to run the following commands:

  1. npm install firebase-functions@latest --save

  2. npm install firebase-admin@latest --save

  3. npm install -g firebase-tools

  4. In index.js I had to make the following change:
    Old
    admin.initializeApp(functions.config().firebase);
    new
    admin.initializeApp();

  5. In sendEmail () I had to update the code to:
    exports.sendEmail = functions.firestore
    .document('orders/{id}')
    .onCreate((event, context) => {
    const { globalConfig } = require('./inits');
    const emailTemplates = require('./templates/email');
    const orderId = event.ref.id;
    const data = event.data();
    data.orderId = orderId;

     const sendToCustomer = sendEmail({
     	from: `${globalConfig.sender_name} <${globalConfig.sender_email}>`,
     	to: data.user.email,
     	subject: 'You order: ' + orderId,
     	text: emailTemplates.customer(data, globalConfig)
     });
     const sendToAdmin = sendEmail({
     	from: `${globalConfig.sender_name} <${globalConfig.sender_email}>`,
     	to: globalConfig.admin_email,
     	subject: 'You have new order ' + orderId,
     	text: emailTemplates.admin(data, globalConfig)
     });
    
     return Promise.all([sendToAdmin, sendToCustomer]);
    

    });

@mutebg
Copy link
Owner

mutebg commented Jun 25, 2018

Thanks, that looks great, can you create PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant