Javascript feetor.users.findOne()返回未定义的,但它';他还在工作
我正在接触meteor.js,我对一些事情很困惑 我写了一个新的收藏:Javascript feetor.users.findOne()返回未定义的,但它';他还在工作,javascript,meteor,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)
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 ======
但在我的浏览器中,我仍然得到一个错误代码:
模拟调用“commentInsert”TypeError的效果时出现异常:无法读取未定义(…)TypeError的属性“emails”:无法读取未定义的属性“emails”
在此处调用该函数:
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
集合中只有当前用户的数据。确保您的服务器端sendmail
是查询Meteor.users
,而不是客户端。Meteor.users在客户端工作,只需确保您已发布并订阅了Meteor.users。手动查找({},{})
。它返回未定义,因为您试图从尚未订阅的源获取数据并发布所有用户的数据,所以并不建议您发布所有用户的数据(出于安全和性能目的)。他可以只发布所需的用户或基于必要的条件发布