Meteor "TypeError: Attempting to access an undefined property value"

Just starting out with Meteor.

I'm working on sending an email notification to new users who are invited to join the service. I want the email content to be personalized with details of the user who sent the invitation. Here is the code snippet I have:

    Meteor.publish('profile', function() {
        return Meteor.users.find(this.userId);
    });

    var emailData = {
        existingUser: 'currentUser().profile.displayName',
        existingOrganisation: 'currentUser().profile.organisation',
        existingEmail: Meteor.users.emails[0].address
    };

    //Customizing enrollment email content as per Meteor Docs example
    Accounts.emailTemplates.siteName = "Amendd";
    Accounts.emailTemplates.from = "Amendd <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e9e8aaf5e2f7ebfec7e6e9e3efeaacffe4e8ea">[email protected]</a>>";
    Accounts.emailTemplates.enrollAccount.subject = function (user) {
        return "Welcome to Amendd";
    };
    Accounts.emailTemplates.enrollAccount.html = function (user, url) {
        return  SSR.render('enrollAccountEmail', emailData) + url;
    };

In my '/private' folder, I have a separate file called enrollAccount.html where the content for enrollment email is stored. The focus right now is on fixing the email field issue, hence placeholders after existingUser and existingOrganisation variables.

Upon saving and running the project in Terminal, I encounter the following error message:

TypeError: Cannot read property '0' of undefined"

This points to the [0] position in the existingEmail variable.

Any insights on how to resolve this problem would be greatly appreciated!

UPDATE

The solution involved reorganizing the emailData object within the function:

            SSR.compileTemplate('enrollAccountEmail', Assets.getText('enrollAccountEmail.html'));
            Accounts.emailTemplates.siteName = "Amendd";
            Accounts.emailTemplates.from = "Amendd <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84eaeba9f6e1f4e8fdc4e5e9e1eae0e0aaebe8e1">[email protected]</a>>";
            Accounts.emailTemplates.enrollAccount.subject = function (user) {
                return "Welcome to Amendd , " + user.email;
            };
            Accounts.emailTemplates.enrollAccount.html = function (user, url) {
            var emailData = {
                existingUser: Meteor.user().profile.displayName,
                existingOrganisation: Meteor.user().profile.organisation,
                existingEmail: Meteor.user().emails[0].address
            };

                return  SSR.render('enrollAccountEmail', emailData) + url;
            };

Answer №1

currentUser() does not seem to function on the server side. You can instead use Meteor.user(), which is effective in all instances except for within a publish function.

To retrieve the email address of the current user, you can use: Meteor.user().emails[0].address

For more information on the Meteor.user() method, you can refer to this documentation:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most efficient way to export data from Excel to a paginated gridview on Yii2?

Good evening to all. I am currently utilizing the Yii2 Framework and facing a challenge with exporting filters to csv using the GridView component from yii2\kartik or the ExportMenu also from yii2\kartik. The issue lies in the fact that my data ...

The process of selecting specific subdocuments

{"categories":4, "students" : [ {name:Sarah, age:26, city:Chicago}, {name:David, age:41, city:Los Angeles} ] } I have numerous data entries similar to this in my database. My objective is to solely extract the initial d ...

Steps to avoid reinitializing the component upon changing routes in an Angular 9 component

In my component, the width of a chart is stored in a variable (because I can't use style for d3). However, every time the route changes, all variables in this class component become undefined. I have tried using ngIf, services (which also become unde ...

Node.js with PostgreSQL (pg): The client connection has already been established. Reusing the client is not allowed

Currently, I am working on creating a basic register/login system and I'm facing an issue with checking for existing usernames. Here are the steps I've followed: Visit the localhost:3000/users/register page Input all required details and click o ...

Track the number of views each month using PHP and MySQL to generate dynamic jQuery charts

I am currently utilizing jQuery with chart.js to display the view counter based on month using PHP and MySql. Each page view is inserted into MySql in the following format : | id | ip | page | date(timestamp) | | 1 | 138.86.20.20 | test.p ...

Tips for redirecting in Vuejs when the input focus is not true

I have a Vue view where the search input automatically focuses when opened. However, if the user clicks away from the input, I want the view to redirect back. I've searched for solutions to this problem but have not had any luck. Here is my code usin ...

org.springframework.beans.factory.UnsatisfiedDependencyException: The creation of the 'restaurantController' bean encountered an error

Yesterday, I successfully built a REST Service using Spring Boot and MongoDB for my app. However, today when I tried to boot it up, I kept encountering an error related to unsatisfied dependencies. Here is the relevant code snippet: GastrorateApplication. ...

Learn how to monitor and analyze hard drive usage and statistics on Windows and Mac systems by utilizing node.js or electronjs

Is there a way to retrieve hard disk utilization, idle time, and HDD type using JavaScript specifically for Windows and MacOS operating systems? I have access to the nodejs and electron API for my application. I have been struggling to find any API or np ...

Can I restrict the translation of the educational institution's name on my website into a different language?

As I develop a website for a training facility, I have encountered an issue with the translated versions of the site. The training center's name is in English, and when the site is translated into another language, it alters the appearance of the name ...

Changing button colors in VueJs

I just started learning VueJS and I'm looking for an example on how to toggle colors between two buttons when they are clicked. For instance, clicking the white button should change it to black while changing the other button to white, and vice versa ...

Access an initialized object in Node.js across all modules

In my app.js file, I have initialized an object that I want to be accessible in all modules. Is passing this object to every module the only way to achieve this? Or is there a more efficient method that I am overlooking? I noticed that mongoose supports d ...

Having trouble importing images in React and passing them as a prop?

I'm attempting to import images, place them into an array, and then pass that array to a prop in a component to display different images. However, after passing the array to the component, the items accessed from the array are showing as undefined, pr ...

creating a multi-page form using HTML and JavaScript

I need help creating a multi-page form with a unique tab display. The first page should not have a tab-pill, while the following pages should display tabs without including the first page tab. Users can navigate to the first page using only the previous b ...

Is there a way to conceal JSON objects within a three.js scene using the loader.load function?

I am working with multiple JSON files in my three.js project and loading them using loader.load. The files are displayed when I click on the corresponding text content. How can I unload or hide an object with a second click? Below is the code I am curren ...

Effective error management in Node.js utilizing Express

What is the best practice for handling server errors and rejecting promises in node/express? A) Approach it like this: res.json({status: 1, message: 'Test missing phone.', error: 'phone'}) Then extract the 'error' from error ...

Encountering unexpected behavior with the .data() method

I'm encountering an issue with my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv= ...

Personalized customizable elements in Draft.js (extension)

For instance, I am interested in enabling users to incorporate a dynamic slideshow of images into their page using Draft.js. This would allow them to select a predefined slideshow from my app and have it automatically update on their page if the images a ...

Assistance needed for jQuery append/prepend function selector

Imagine you have the following HTML code: <div> <span></span> <span></span> <span></span> </div> Is there a way to add text before the last span tag? Here's an example: <div> <span>< ...

The jQuery functions are unable to function properly when retrieving AJAX data

I am currently working on a script that inserts a record into my database using AJAX. After inserting the data, it is posted back in the following format... Print "<li>"; Print "<span class='cost'>" . $bill. "</span> "; Print ...

The functionality of Vue's keep-alive feature seems to be malfunctioning within Quasar, as the mounted hook is consistently triggered when using

I am working on a Quasar application where I want to implement page caching using keep-alive in a specific scenario. The scenario is as follows: the user goes from the home page to Page 1, then navigates to Page 2, and finally returns to Page 1 using $rout ...