online-store icon indicating copy to clipboard operation
online-store copied to clipboard

SendEmail: Cannot read property 'id' of undefined

Open ghost opened this issue 6 years ago • 4 comments

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)

ghost avatar Jun 21 '18 23:06 ghost

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.

mutebg avatar Jun 23 '18 06:06 mutebg

Hello Sir!

Thank You for coming back to me on this.

So far I have found that event.ref.id gives you order id. However event.data doesn’t give you the order object.

Would be great if you find a solution.

On Sat, 23 Jun 2018 at 07:48, Stoyan Delev [email protected] wrote:

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mutebg/online-store/issues/15#issuecomment-399642062, or mute the thread https://github.com/notifications/unsubscribe-auth/AjgDppnNJLJsScyeRlDMeiYEYaB-vlFhks5t_eTWgaJpZM4Uy76y .

ghost avatar Jun 23 '18 07:06 ghost

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]);
    

    });

ghost avatar Jun 24 '18 21:06 ghost

Thanks, that looks great, can you create PR?

mutebg avatar Jun 25 '18 06:06 mutebg