javascript - Meteor.users.findOne() 返回未定义但它仍在工作

标签 javascript meteor

我正在接触 meteor.js,但我对某些事情感到很困惑。

我写了一个新的合集:

sendEmailAfterNotification = function(comment) {

  var post = Posts.findOne(comment.postId);

  if (comment.userId !== post.userId) {

    Meteor.call('sendEmail',
                Meteor.users.findOne(post.userId).emails[0].address,
                'automailer@yourdomain.com',
                comment.author + ' answered on your post : ' + post.title,
                'random bodytext');

  }
};

我的 meteor 控制台输出完全没问题我会得到:

I20160424-16:00:54.758(2)? ====== BEGIN MAIL #0 ======
I20160424-16:00:54.766(2)? (Mail not sent; to enable sending, set the MAIL_URL environment variable.)
I20160424-16:00:54.767(2)? MIME-Version: 1.0
I20160424-16:00:54.767(2)? From: automailer@yourdomain.com
I20160424-16:00:54.767(2)? To: test@testdomain.com
I20160424-16:00:54.767(2)? Subject: testuser answered on your post : foobar
I20160424-16:00:54.767(2)? Content-Type: text/plain; charset=utf-8
I20160424-16:00:54.767(2)? Content-Transfer-Encoding: quoted-printable
I20160424-16:00:54.767(2)? 
I20160424-16:00:54.768(2)? random body text
I20160424-16:00:54.768(2)? ====== END MAIL #0 ======

但在我的浏览器中我仍然收到错误代码:

Exception while simulating the effect of invoking 'commentInsert' TypeError: Cannot read property 'emails' of undefined(…) TypeError: Cannot read property 'emails' of undefined

函数在这里被调用: comments.js

Comments = new Mongo.Collection('comments');

Meteor.methods({
  commentInsert: function(commentAttributes) {
  check(this.userId, String);
  check(commentAttributes, {
  postId: String,
  body: String
});

var user = Meteor.user();
var post = Posts.findOne(commentAttributes.postId);

if (!post)
  throw new Meteor.Error('invalid-comment', 'You must comment on a post');

comment = _.extend(commentAttributes, {
  userId: user._id,
  author: user.username,
  submitted: new Date()
});

// update the post with the number of comments
Posts.update(comment.postId, {$inc: {commentsCount: 1}});

// create the comment, save the id
comment._id = Comments.insert(comment);

// now create a notification, informing the user that there's been a comment
createCommentNotification(comment);

// send an email to the post owner - if it's not the owner
sendEmailAfterNotification(comment);

return comment._id;
}
});

最佳答案

Meteor.users.findOne(post.userId) 在客户端不起作用,因为客户端只有当前用户的数据在 Meteor.users 集合中。确保您的服务器端 sendEmail 是查询 Meteor.users 的服务器端,而不是客户端。

关于javascript - Meteor.users.findOne() 返回未定义但它仍在工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824219/

相关文章:

meteor - Material-ui 错误(缺少类属性转换。)

javascript - Meteor.js 应用程序中 Iron 路由器路由的真正等效值 `window.onload` 是什么?

javascript - ES6 - 克隆一个对象并在一行中重命名它的键?

javascript - 如何使用 Marionette.Callbacks

javascript - jQuery timepicker 不能在模态中工作

node.js - 内存泄漏 Meteor.http

sqlite - 如何在 MeteorJS 中使用 sqlite 数据库?

javascript - 什么数据结构/算法可用于在 JavaScript 中检索一对值(之前和之后)

.net - 从 JSON 网络服务返回 HTML - ".d"是什么?

javascript - 当 web 端口 80 已被使用时,如何监听 heroku 上的任何端口?