How do i access the nested field from a document in Mongodb


#1

Here is the data:

{
	"_id" : "123",
	"createdAt" : ISODate("2018-01-19T18:06:03.060Z"),
	"services" : {
		"servicenamw" : {
			"id" : 8217234,
			"accessToken" : "22234354",
			"email" : "test@gmail.com",
			"username" : "test",
			"emails" : [
				{
					"email" : "test@gmail.com",
					"primary" : true,
					"verified" : true,
					"visibility" : "private"
				},
				{
					"email" : "random",
					"primary" : false,
					"verified" : true,
					"visibility" : null
				},
				{
					"email" : "blob",
					"primary" : false,
					"verified" : true,
					"visibility" : null
				}
			]
		},
		"resume" : {
			"loginTokens" : [
				{
					"when" : ISODate("2018-01-19T18:06:03.074Z"),
					"hashedToken" : "qwerty43234"
				}
			]
		}
	}
}

Here is the meteor code

<template name ="user">
	{{UserName}}
</template>

Template.user.helpers({
	 UserName() {

	 return Meteor.userId().services.servicename.username;
	 
	}
});

It does not return anything


#2

Just taking a quick look, maybe it is a typo? You have “servicenamw” not “servicename”.

–jw


#3

oh but that i changed it, sorry about that but its not a type issue


#4

You are using Meteor.userId() when you should use Meteor.user()

Meteor.user().services.servicename.username;


#5

The issue is still not resolved, is there a problem with the template?


#6

It only returns that data if you publication returns that data. So check your publication.


#7

could you check what console.dir(Meteor.user()); is giving you?


#8

Update : The issue is resolved!!!, I had some issues on the server(Meteor.publish()), thanks @chris74656, for the correction


#9

yeah!! my bad, thanks for your help