A
A
Alecxandrys2016-04-28 20:43:52
JavaScript
Alecxandrys, 2016-04-28 20:43:52

How to resolve Exception in Meteor.js when opening main page?

Good afternoon,
When you open the main page, two exceptions fall out in the log and the page is not loaded to the end.
Log:

I20160428-20:30:52.505(3)? Exception from sub battles id ZYdctF5sKwL4ueJaw TypeError: Cannot read property 'username' of undefined
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 undefined
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

A piece of Publish.js that these actions refer to. 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 have been displayed on the main page is preserved.
The problem is fixed if the user logs in, but if he logs out, the main page is only partially displayed again

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2016-04-28
@Alecxandrys

The problem is fixed if the user logs in, but if he logs out, the main page is only partially displayed again

Obviously, if the user is not logged in, then it is this.userIdempty and the request Meteor.users.findOnereturns nothing. And then you try to use the data from this nothing , access its fields. This is exactly what you need to check:
if (!currentUser) {
  return [];
}

Or something like that.
PS The only time it's excusable to use "x" as a variable name is when it stores a coordinate.

V
Vitaly Safonov, 2015-05-20
@squirtazzer

Spali WC Lite plugin can work with fields https://wordpress.org/plugins/saphali-woocommerce-lite/
or you can use https://wordpress.org/plugins/woocommerce-poor-guy...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question