2016-04-24 3 views
3

Я получаю связь с meteor.js, и я довольно смущен некоторыми вещами.Meteor.users.findOne() возвращает undefined, но он все еще работает

Я написал новую коллекцию:

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, 
       '[email protected]', 
       comment.author + ' answered on your post : ' + post.title, 
       'random bodytext'); 

    } 
}; 

мой выход метеор консоль совершенно нормально, я получить:

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: [email protected] 
I20160424-16:00:54.767(2)? To: [email protected] 
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 ====== 

, но в моем браузере я все еще получаю ERRORCODE:

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

функция вызывается здесь: комментарии.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; 
} 
}); 

ответ

2

Meteor.users.findOne(post.userId) не работает на стороне клиента, так как клиент имеет только данные текущего пользователя в Meteor.users коллекции. Убедитесь, что серверная сторона sendEmail - это тот, который запрашивает Meteor.users, а не клиент.

Смежные вопросы