online-store
online-store copied to clipboard
SendEmail: Cannot read property 'id' of undefined
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.
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.
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.
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 .
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:
-
npm install firebase-functions@latest --save
-
npm install firebase-admin@latest --save
-
npm install -g firebase-tools
-
In index.js I had to make the following change: Old admin.initializeApp(functions.config().firebase); new admin.initializeApp();
-
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]);
});
Thanks, that looks great, can you create PR?