1

everyone. I would like the students who enroll a subject are shown in a table when this subject is selected in a dropdown list. The ID of these students is stored in an array. The problem is that this ID array retrieved from the document looks kind of strange. seem like there is an array in an array. Like this shown in the console: shown in console

 {enrollment: Array(2)}
     enrollment: Array(2)
     0: "b1602231"   
     1: "B1560124"   
     length: 2 
     __proto__: Array(0)
     __proto__: Object

And it throwed an error: Exception in template helper: Error: $in needs an array

So how could I solve this? I would really appreciate it if someone can give me some idea.

Below is the event handler and helper.

Template.subject.events({
  ‘change #dropdown’: function(event, template) {
    var selectedValue = $(event.target).val();
    var array = subject.findOne( { subjectCode:selectedValue }, { fields:{ _id:0, enrollment:1 } } );
    Session.set(‘studentEnrolled’,array);
  }
});

Template.student.helpers({
  student: function() {
    var listOfStudent = Session.get( ‘studentEnrolled’ );
    return student.find( { studentID: { $in:listOfStudent } } );
  }
});

//HTML

<template name="student">
        {{#each student}}
        <tr>
          <td>{{name}}</td>
        </tr>
        {{/each}}
</template>
5
  • Are you sure there's an array within an array? From what I can see it looks like there is just a single array in the console output. Commented Mar 29, 2020 at 13:03
  • @Leafyshark I am not sure about that. Because that __proto__ attribute confuses me. Commented Mar 29, 2020 at 13:48
  • please can you edit your post with a screenshot of the console.log instead of copy/paste? Commented Mar 29, 2020 at 14:00
  • @Leafyshark Sure, I just uploaded it Commented Mar 29, 2020 at 14:05
  • Okay, it's just a single array. Commented Mar 29, 2020 at 14:37

2 Answers 2

1

Copying my answer from the forums:

First of all, you are taking the whole subject document (and after the first answer, wrapping that array into another array) here:

Session.set('studentEnrolled',[array]);

Which means when you search here:

return student.find({studentID:{$in:listOfStudent}});

You are passing an array with a document in it, instead of the enrollments array.

What you want to do is store the enrollments in the session:

Session.set('studentEnrolled', array.enrollments);

I'd also recommend renaming the variable array since it's not an array, and that probably contributed to your confusion

Sign up to request clarification or add additional context in comments.

Comments

1

Find get one records from data. u can convert in array using [data]

var array = subject.findOne({subjectCode:selectedValue}, {fields:{_id:0, enrollment:1}});
Session.set(‘studentEnrolled’,[array]);
}

// For async update More: https://blog.meteor.com/using-promises-and-async-await-in-meteor-8f6f4a04f998

Template.hello.onCreated(function helloOnCreated() {
  this.list = new ReactiveVar([]);
  Meteor.call('getHobbits', (error, result) => {
    this.list.set(result);
  });
});

Template.hello.helpers({
  hobbits() {
    return Template.instance().list.get();
  },
});

4 Comments

Thanks.That error is fixed. But students are not showing in the table. Any idea?
Thanks, I will check it out.
Ig helps. please upvoate :-D
I wish I could but I have to earn enough reputation to upvote :( Sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.