Acess A Collection List

Hi Guys,

I am struggling on accessing a collection list:

the seed is such:

       var students = [
           	{
	           		student:{
		           		firstname:"Cinar",
		           		lastname: "Can",
		           		parent: "Canar",
		           		teacher:["Gizem","Laurent"],
		           		class:["Mavi","English"],
		                stu_id:"masal_01"
	           	}
           	}
   ]

I basically want to student.teacher but only one element in the lists to compare it in the template with another value.

Is there any more easy way to do this?

Thank You So Much
Laurent,

Sorry, Iā€™m having trouble understanding what you want to do?

For this data structure you can access the teacher array like so:
students[0].student.teacher

1 Like

Correct but the output giving me a list of:

Gizem, Laurent

I want only one. How can I access to the first teacher name or the second teacher name randomly.

Thank you so much @coagmano for the quick reply.

teacher is an array, so you can access via the bracket accessor:
First: students[0].student.teacher[0]
Second: students[0].student.teacher[1]
Random: students[0].student.teacher[Math.round(Math.random())]
Random for more than two:

const randomNumBetween = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const teachers = students[0].student.teacher;
const randomIndex = randomNumBetween(0, teachers.length);
const randomTeacher = teachers[randomIndex];

You should think about refactoring this structure if you can, the extra student key seems redundant and you are mixing plural and singular names with a mixture of values and arrays

1 Like

Basicaly, this is my seed colection

if (Student.find().count() == 0) {
// console.log(ā€œImporting private/Students.json to dbā€)
// var data = JSON.parse(Assets.getText(ā€œstudents.jsonā€));

       var students = [
           	{
	           		student:{
		           		firstname:"Cinar",
		           		lastname: "Can",
		           		parent: "Canar",
		           		teacher:["Gizem","Laurent"],
		           		class: ["Mavi"."English"],
		                stu_id:"masal_01"
	           	}
           	},
           	{
           			student:{
		           		firstname:"alper",
		           		lastname: "alpastan",
		           		parent: "Tunar",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_02"
		           	}
           	},
           	{
           			student:{
		           		firstname:"alper2",
		           		lastname: "alpastan2",
		           		parent: "Tunasta",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_03"
           	      }
           	},
           {
           			student:{
			       		firstname:"selin",
			       		lastname: "selin",
			       		parent: "Tunar",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_04"
			       	}
            },
            {
	       			student:{
			       		firstname:"alper",
			       		lastname: "alpastan",
			       		parent: "Mehmet",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_05"
			       	}
           }
    ]; 

students.forEach(function (item) {
    Student.insert({
    	student:item.student
    });
});

};

And here is my publlish

Template.parent.helpers({
thisstudent: function(){
var this_student = Student.find({}, {fields: {ā€˜studentā€™: true}});
// console.log(student.firstname);
return this_student;
},
this_student_id: function(){
var this_id = Template.parentData(1)._id;
var this_student = Student.findOne({_id: this_id});
// console.log(student.firstname);
return this_student;
},
student_image: function(){
return Images.find();
},
author: function(){
// console.log(hjsdn);
return Meteor.users.findOne({_id: this.metadata.owner});
}
});

And here is Template

<div class="parent-page ui-view  main" key="parent">    

 {{#each thisstudent}}
   {{#if isEqual currentUser.username  student.parent}}
    	<h2>{{student.firstname}} <small>Class</small></h2> 

      {{#each student_image}}
          {{#if this_student_id}}
             {{#if isEqual this_student_id._id metadata.student}}
             
               {{#if isEqual author.profile.class this_student_id.student.class}} 

				<button class="accordion fa fa-star">
			      Mavi Sinif
				</button>
				<div class="panel">
                  <img class="activator" id="stu_image" src="{{this.url store='images'}}" width="250" height="270">	
                  <br>				     
	  			</div>

	  		  {{else}}

	  		   <button class="accordion fa fa-star">
			    Mavi Sinif
				</button>
				<div class="panel">
                 No picture added Today. Please Contact the teacher for more informations
                  <br>				     
	  			</div>
	  		   {{/if}}


           {{/if}} 
         {{/if}}
         {{else}}
         <div>
          <i style="margin-top: 30%; margin-left: 40%" class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
       {{/each}}
     {{/if}}
 {{/each}} 
</div>

And i am trying to compare if the current Student has one of the Class. :frowning: @coagmano

You can format code blocks on the forum by surrounding it in triple backticks ``` which will make it much easier for us to read

How do i do that? @coagmano

Basicaly, this is my seed colection

if (Student.find().count() == 0) {
// console.log(ā€œImporting private/Students.json to dbā€)
// var data = JSON.parse(Assets.getText(ā€œstudents.jsonā€));

       var students = [
           	{
	           		student:{
		           		firstname:"Cinar",
		           		lastname: "Can",
		           		parent: "Canar",
		           		teacher:["Gizem","Laurent"],
		           		class: ["Mavi"."English"],
		                stu_id:"masal_01"
	           	}
           	},
           	{
           			student:{
		           		firstname:"alper",
		           		lastname: "alpastan",
		           		parent: "Tunar",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_02"
		           	}
           	},
           	{
           			student:{
		           		firstname:"alper2",
		           		lastname: "alpastan2",
		           		parent: "Tunasta",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_03"
           	      }
           	},
           {
           			student:{
			       		firstname:"selin",
			       		lastname: "selin",
			       		parent: "Tunar",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_04"
			       	}
            },
            {
	       			student:{
			       		firstname:"alper",
			       		lastname: "alpastan",
			       		parent: "Mehmet",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_05"
			       	}
           }
    ]; 

students.forEach(function (item) {
    Student.insert({
    	student:item.student
    });
});

};

And here is my publlish

Template.parent.helpers({
thisstudent: function(){
var this_student = Student.find({}, {fields: {ā€˜studentā€™: true}});
// console.log(student.firstname);
return this_student;
},
this_student_id: function(){
var this_id = Template.parentData(1)._id;
var this_student = Student.findOne({_id: this_id});
// console.log(student.firstname);
return this_student;
},
student_image: function(){
return Images.find();
},
author: function(){
// console.log(hjsdn);
return Meteor.users.findOne({_id: this.metadata.owner});
}
});

And here is Template

<div class="parent-page ui-view  main" key="parent">    

 {{#each thisstudent}}
   {{#if isEqual currentUser.username  student.parent}}
    	<h2>{{student.firstname}} <small>Class</small></h2> 

      {{#each student_image}}
          {{#if this_student_id}}
             {{#if isEqual this_student_id._id metadata.student}}
             
               {{#if isEqual author.profile.class this_student_id.student.class}} 

				<button class="accordion fa fa-star">
			      Mavi Sinif
				</button>
				<div class="panel">
                  <img class="activator" id="stu_image" src="{{this.url store='images'}}" width="250" height="270">	
                  <br>				     
	  			</div>

	  		  {{else}}

	  		   <button class="accordion fa fa-star">
			    Mavi Sinif
				</button>
				<div class="panel">
                 No picture added Today. Please Contact the teacher for more informations
                  <br>				     
	  			</div>
	  		   {{/if}}


           {{/if}} 
         {{/if}}
         {{else}}
         <div>
          <i style="margin-top: 30%; margin-left: 40%" class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
       {{/each}}
     {{/if}}
 {{/each}} 
</div>

Still not sure what you are trying to achive but;

class: ["Mavi"."English"], I think you need a comma there istead of dot.

{{#each thisstudent}}
  {{#if isEqual currentUser.username  student.parent}}

Looks like you are cycling through all students and displaying only for current user. If so;
change you helper (also publication if necessary)

thisstudent: function(){
    var this_student = Student.findOne({_id:Meteor.userId()}, {fields: {ā€˜studentā€™: true}});
    return this_student;
},

and change your template

{{#with student}}
   <h2>{{firstname}} <small>Class</small></h2>

I suggest not to make a habit of using Template.parentData(n)._id It may easily get mixed what level of parent if you want to write reusable templates.

{{> StudentTemplate student=thisstudent}}

And in the template code

Template.instance().data.student;
// or
Template.currentData().student;
2 Likes

Start a line with three backticks, paste your code, then finish off on a new line with three backticks:

```
paste
code
here
```
2 Likes
if (Student.find().count() == 0) {
// console.log(ā€œImporting private/Students.json to dbā€)
// var data = JSON.parse(Assets.getText(ā€œstudents.jsonā€));

       var students = [
           	{
	           		student:{
		           		firstname:"Cinar",
		           		lastname: "Can",
		           		parent: "Canar",
		           		teacher:["Gizem","Laurent"],
		           		class: ["Mavi"."English"],
		                stu_id:"masal_01"
	           	}
           	},
           	{
           			student:{
		           		firstname:"alper",
		           		lastname: "alpastan",
		           		parent: "Tunar",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_02"
		           	}
           	},
           	{
           			student:{
		           		firstname:"alper2",
		           		lastname: "alpastan2",
		           		parent: "Tunasta",
		           		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
		                stu_id:"masal_03"
           	      }
           	},
           {
           			student:{
			       		firstname:"selin",
			       		lastname: "selin",
			       		parent: "Tunar",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_04"
			       	}
            },
            {
	       			student:{
			       		firstname:"alper",
			       		lastname: "alpastan",
			       		parent: "Mehmet",
			       		teacher:["Gizem","Laurent"],
		                class: ["Mavi"."English"],
			            stu_id:"masal_05"
			       	}
           }
    ]; 

students.forEach(function (item) {
    Student.insert({
    	student:item.student
    });
});
};

And here is my publlish

Template.parent.helpers({
thisstudent: function(){
var this_student = Student.find({}, {fields: {ā€˜studentā€™: true}});
// console.log(student.firstname);
return this_student;
},
this_student_id: function(){
var this_id = Template.parentData(1)._id;
var this_student = Student.findOne({_id: this_id});
// console.log(student.firstname);
return this_student;
},
student_image: function(){
return Images.find();
},
author: function(){
// console.log(hjsdn);
return Meteor.users.findOne({_id: this.metadata.owner});
}
});

And here is Template

<div class="parent-page ui-view  main" key="parent">    

 {{#each thisstudent}}
   {{#if isEqual currentUser.username  student.parent}}
    	<h2>{{student.firstname}} <small>Class</small></h2> 

      {{#each student_image}}
          {{#if this_student_id}}
             {{#if isEqual this_student_id._id metadata.student}}
             
               {{#if isEqual author.profile.class this_student_id.student.class}} 

				<button class="accordion fa fa-star">
			      Mavi Sinif
				</button>
				<div class="panel">
                  <img class="activator" id="stu_image" src="{{this.url store='images'}}" width="250" height="270">	
                  <br>				     
	  			</div>

	  		  {{else}}

	  		   <button class="accordion fa fa-star">
			    Mavi Sinif
				</button>
				<div class="panel">
                 No picture added Today. Please Contact the teacher for more informations
                  <br>				     
	  			</div>
	  		   {{/if}}


           {{/if}} 
         {{/if}}
         {{else}}
         <div>
          <i style="margin-top: 30%; margin-left: 40%" class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
       {{/each}}
     {{/if}}
 {{/each}} 
</div>

@coagmano

First comments are the same as @baris

Second, your template contains an unclosed each: {{#each student_image}}
And an orphaned /each close tag after the spinner icon

Third, the variable names are very hard to follow, and I think this is related to the over-complicated data structure I mentioned in an earlier post. This component can and should be greatly simplified. Which should help make it less bug prone too.

Example: a variable called this_student_id should not be an object with properties, it should be the id value.

Now as for the question ā€œi am trying to compare if the current Student has one of the Classā€
Iā€™m guessing youā€™re referring to this comparison:

{{#if isEqual author.profile.class this_student_id.student.class}}

Which I can see will never be true since student.class is an array. Array comparison does an identity check and will only be true if it is a pointer to the same variable. Example:

['a'] === ['a'] // false
var b = ['b'];
b === ['b'] // false
var c = b;
b === c // true

You will need a new helper that checks values inside the array

Template.registerHelper('isInsideArray' (value, array) => array.includes(value))

{{#if isInsideArray author.profile.class this_student_id.student.class}}
1 Like

Thank you so much @coagmano, that is exactly what I was solving for. I solved it in the similar way you did :heart::blush::blush:

Thank you Guys. Every comment has been helping me solving this issue. Also learned new way to cope with the parent data issue thank to @baris.

Thank you so much Guys.