【问题标题】:Meteor "TypeError: Cannot read property '0' of undefined"流星“TypeError:无法读取未定义的属性'0'”
【发布时间】:2016-02-16 17:31:04
【问题描述】:

我是 Meteor 的新手。

我想做的是在某人被邀请加入服务(完成)时向他们发送一封电子邮件,但我希望该电子邮件动态填充邀请他们加入的用户的详细信息。这是我的代码:

    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
    };

    //Code taken from Meteor Docs to customise content of enrollment email
    Accounts.emailTemplates.siteName = "Amendd";
    Accounts.emailTemplates.from = "Amendd <no-reply@amendd.com>";
    Accounts.emailTemplates.enrollAccount.subject = function (user) {
        return "Welcome to Amendd";
    };
    Accounts.emailTemplates.enrollAccount.html = function (user, url) {
        return  SSR.render('enrollAccountEmail', emailData) + url;
    };

enrollAccount.html 是我的“/private”文件夹中的一个单独文件。我在我的 existingUser 和 existingOrganisation 变量之后放置了“--”,而我专注于让电子邮件字段工作。

在终端中保存并运行项目时出现的错误消息是

TypeError: 无法读取未定义的属性“0”

它将我指向现有电子邮件变量中的 [0]。

谁能解释我遇到的问题?

更新

解决此问题的方法是在函数中移动 emailData 对象:

            SSR.compileTemplate('enrollAccountEmail', Assets.getText('enrollAccountEmail.html'));
            Accounts.emailTemplates.siteName = "Amendd";
            Accounts.emailTemplates.from = "Amendd <no-reply@amendd.com>";
            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;
            };

【问题讨论】:

  • 错误是说 Meteor.users.emails 未定义。确保您的数据库或任何其他数据提供者中有此字段。例如,为什么获取现有用户与现有电子邮件不同?
  • 不应该是currentUser().emails[0].address吗?
  • 我最初尝试 currentUser().emails[0].address 并抛出错误“ReferenceError: currentUser is not defined”,existingUser 和 existingOrganisation 仍然是这样的原因是因为我删除了一个引用任何一方,看看我是否可以通过使用 SSR 提取任何数据(确实如此)
  • 如何获取推荐人的电子邮件地址/用户 ID?它存储在某个地方吗?为什么要在函数之外构造emailData 对象?
  • 推荐人的电子邮件地址是他们创建帐户时注册的电子邮件地址,存储在 Meteor 帐户包中。我在客户端返回此信息没有问题,它只是在服务器端。我在放置 emailData 对象时犯了错误,我现在将它移到了函数中,但仍然出现同样的错误。

标签: javascript mongodb meteor


【解决方案1】:

我不相信 currentUser() 在服务器上工作。尝试使用 Meteor.user(),它可以在除发布功能之外的任何地方工作。

Meteor.user().emails[0].address

http://docs.meteor.com/#/full/meteor_user

【讨论】:

  • 谢谢山姆!完成了这项工作 - 我之前尝试过这个,但我在函数之外定义了 emailData 对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 2019-08-04
  • 2015-06-08
  • 1970-01-01
相关资源
最近更新 更多