Exception from sub books id dMBTck6CaSupWbEna TypeError: Cannot read property 'emails' of null

I am getting an Exception whenever a user logged in into his account, and I cant see the collection data until the page is been refreshed… **[but I am not getting any exception if i use it like
’’’

Books.find({“customer_name”:this.userId);

‘’'
instead of

‘’’

Books.find({“customer_name”:Meteor.user().emails[0].address);

‘’'
and i dont need that step because in my app users collection finding should be based on there email id not by userId…]**
my code is shown below :
Some one please help me out :frowning_face:

server - main.js:

‘’’

var My_collection;
Meteor.publish(‘books’, function() {
My_collection = Books.find({“customer_name”:Meteor.user().emails[0].address);
return My_collection;

‘’’

client -main.js:

‘’’

Template.viewBooks.helpers({
books() {
return Books.find({}, { sort: { books_order: -1 } }).fetch();
},
});

‘’’

client - main.html

‘’’

{{#each books}} {{books_id}} {{/each}}

‘’’

ERROR:

App running at: http://localhost:3000/
I20171205-16:06:49.773(5.5)? Exception from sub transactions id wQ9DpjjABEiPzMZ9P TypeError: Cannot read property ‘emails’ of null
I20171205-16:06:49.872(5.5)? at Subscription._handler (server/main.js:16:75)
I20171205-16:06:49.873(5.5)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1768:12)
I20171205-16:06:49.873(5.5)? at DDP.CurrentPublicationInvocation.withValue (packages/ddp-server/livedata_server.js:1043:15)
I20171205-16:06:49.873(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1134:15)
I20171205-16:06:49.873(5.5)? at Subscription.runHandler (packages/ddp-server/livedata_server.js:1041:51)
I20171205-16:06:49.873(5.5)? at packages/ddp-server/livedata_server.js:826:41
I20171205-16:06:49.874(5.5)? at Function.
.each.
.forEach (packages/underscore.js:147:22)
I20171205-16:06:49.874(5.5)? at packages/ddp-server/livedata_server.js:822:9
I20171205-16:06:49.874(5.5)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1134:15)
I20171205-16:06:49.874(5.5)? at Session._setUserId (packages/ddp-server/livedata_server.js:816:34)

Please wrap your code between lines with triple backticks:

```
like
this
```
2 Likes

You can’t use Meteor.user() or Meteor.userId() inside of a subscription, which is why it attaches the userId to the context.

Try fetching the user first, and then using it like this.

const currentUser = Meteor.users.find(this.userId);
return Books.find({ customer_name: currentUser.emails[0].address })
2 Likes

@copleykj It didn’t work :disappointed:

the new error was

Exception from sub transactions id b7tdroL6urNFtzT3h TypeError: Cannot read property ‘0’ of undefined

and I was not getting data even when i refresh the page

just check if currentUser is actually a user structure and contains a valid array email. You get the error because you are trying to access an array element when there is no array in the structure. I recommend to console.log(currentUser) in your code