How to resolve Exception in Meteor.js when you open the FrontPage?

Good day,
When you open the main page in the log falls in two explana and the page does not loads in addition to the end.
Log:
I20160428-20:30:52.505(3)? Exception from sub battles id ZYdctF5sKwL4ueJaw TypeError: Cannot read property 'username' of undefin
ed
I20160428-20:30:52.506(3)? at [object object].Accounts.onCreateUser.user.profile [as _handler] (server/Publish.js:35:1)
I20160428-20:30:52.506(3)? at maybeAuditArgumentChecks (livedata_server.js:1698:12)
I20160428-20:30:52.506(3)? at [object object]._.extend._runHandler (livedata_server.js:1023:17)
I20160428-20:30:52.507(3)? at [object object]._.extend._startSubscription (livedata_server.js:842:9)
I20160428-20:30:52.507(3)? at [object object]._.extend.protocol_handlers.sub (livedata_server.js:614:12)
I20160428-20:30:52.507(3)? at livedata_server.js:548:43
I20160428-20:30:52.630(3)? Exception from sub battles id cNsHcdHbyTY2jqJLr TypeError: Cannot read property 'username' of undefin
ed
I20160428-20:30:52.630(3)? at [object object].Accounts.onCreateUser.user.profile [as _handler] (server/Publish.js:35:1)
I20160428-20:30:52.630(3)? at maybeAuditArgumentChecks (livedata_server.js:1698:12)
I20160428-20:30:52.631(3)? at [object object]._.extend._runHandler (livedata_server.js:1023:17)
I20160428-20:30:52.631(3)? at [object object]._.extend._startSubscription (livedata_server.js:842:9)
I20160428-20:30:52.631(3)? at [object object]._.extend.protocol_handlers.sub (livedata_server.js:614:12)
I20160428-20:30:52.631(3)? at livedata_server.js:548:43


Piece Publish.js referenced data acceptany. Specifically, return:
Meteor.publish('battles', function()
{
 var x = Meteor.users.findOne({_id: this.userId});
 return battles.find({$or: [{name1: x.username}, {name2: x.username}]}, {
 fields: {
 name1 : 1,
 name2 : 1,
 battleID: 1,
 BS : 1
}
});
});

The functionality of the elements that had to appear on the main page is saved.
The problem is eliminated if the user Selaginella, but if he goes out, home again is displayed only partially
July 12th 19 at 17:06
1 answer
July 12th 19 at 17:08
Solution
The problem is eliminated if the user Selaginella, but if he goes out, home again is displayed only partially

Obviously, if the user is not logged in this.userId is empty and the query Meteor.users.findOne returns nothing. And then you data from that anything trying to use fields to refer to. This is what you need to check:
if (!currentUser) {
 return [];
}

Or something like that.

P. S. the Only time it's forgivable to use "x" as a variable name - when it holds the coordinate.
Thanks, I forgot about it in a series of Publica and did not notice that not prescribed check - Jayne_Boehm commented on July 12th 19 at 17:11

Find more questions by tags JavaScriptMeteor