Cannot pass array object from map function as argument javascript

jestrabikr

I am trying to figure out why I cannot pass array object from map function as argument to function createModal(student) that is called onclick:
The error is caused by the parameter because when I tried function createModal() it worked fine.

const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
result.innerHTML = "";
counter = 0;

data.forEach((workshop) => {
  ...
  ${workshop.students.map(student =>
     `<li class="list-group-item">
        <a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${student})">${student.first_name} ${student.last_name}</a>
      </li>`
  ).join("")}
  ...
});

I have seen this (and this) question and tried changing the code as following but it did not work either:

...
${workshop.students.map(function(student) {
   var studentCopy = JSON.parse(JSON.stringify(student));
   return `<li class="list-group-item">
             <a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${studentCopy})">${student.first_name} ${student.last_name}</a>
           </li>`
}).join("")}
...

Unfortunately I don't know the exact error because the only thing I see in inspect in Chrome is this. Which is at the beggining of index file and does not help me.

Uncaught SyntaxError: Unexpected identifier (at (index):1:21)

EDIT: I have updated the code to look as following. It kinda works but the problem is that is have a table for workshops and every row has a list with the students inside and instead of appending students to the right workshop it appends them to the first list other are empty.

const workshopList = document.querySelector(".workshop-list");

async function getData(){
  const allWorkshops = await fetch("/api/workshops/detail");
  const data = await allWorkshops.json();
  data.forEach((workshop) => {
      var studentList = document.querySelector(".student-list");
      const tr = document.createElement("tr");
      tr.classList.add("responsive-table__row");
      tr.innerHTML = `
          <td class="responsive-table__body__text responsive-table__body__text--name">
              <ul class="list-group student-list">
                  ${workshop.students.forEach(student => {
                      let li = document.createElement('li');
                      li.classList.add('list-group-item');
                      let anchor = document.createElement('a');
                      anchor.dataset.bsToggle = 'modal';
                      anchor.dataset.bsTarget = '#studentModal';
                      anchor.setAttribute('type', 'button');
                      anchor.addEventListener('click', () => createModal(student));
                      anchor.innerText = student.last_name;
                      li.appendChild(anchor);
                      studentList.appendChild(li);
                  })}
              </ul>
          </td>
      `
      workshopList.appendChild(tr);
  });
};
Barmar

student is an object. When you try to interpolate it into the template literal, it gets turned into the string [Object object], which isn't a meaningful argument to create_modal().

Instead of interpolating, you should create the element using DOM methods, and use addEventListener() with a closure to bind the click listener.

data.forEach((workshop) => {
  //...
  workshop.students.forEach(student => {
    let li = document.createElement('li');
    li.classList.add('list-group-item');
    let anchor = document.createElement('a');
    anchor.dataset.bsToggle = 'modal';
    anchor.dataset.bsTarget = '#studentModal';
    anchor.setAttribute('type', 'button');
    anchor.addEventListener('click', () => createModal(student));
    anchor.innerText = student.last_name;
    li.appendChild(anchor);
    studentList.appendChild(li);
  });
  //...
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javaScript - pass object as function argument

From Dev

Cannot pass an array of object to a function?

From Dev

cannot pass array in a function javascript

From Dev

Unable to pass an array as function argument in Javascript

From Dev

React .map is not a function but cannot pass array

From Dev

Cannot pass an array to a function from a multidimensional array

From Dev

Pass function with one argument to array.map using jsfuck

From Dev

array_map and pass 2 arguments to the mapped function - array_map(): Argument #3 should be an array

From Dev

passing array as javascript function argument from php

From Dev

javascript/typescript - force to pass object as function argument by eslint rule

From Dev

JavaScript throws: "is not a function" when I pass object as argument

From Dev

Javascript function of object as argument

From Dev

Return Object from map() function not Array

From Dev

How to pass array as an argument to a Ruby function from command line?

From Dev

How to pass an array as function argument?

From Dev

How to pass an array argument to a function

From Dev

Pass in part of an array as function argument

From Dev

Pass numpy array as function argument

From Dev

javascript - unused argument 'idtype' although called from a .map() function

From Dev

Map New array of object from array of object javascript

From Dev

Bash - pass argument from array

From Dev

Pass an object into an array in JavaScript

From Dev

Pass argument to flask from javascript

From Dev

Map array js - how to pass argument

From Dev

Cannot pass dictionary as an argument to a VBA function

From Dev

Why cannot pass function name as argument in this case?

From Dev

Cannot pass struct pointer as function argument in C

From Dev

Map object with array in javascript

From Dev

Pass a function with parameter as an argument to another function in JavaScript

Related Related

  1. 1

    javaScript - pass object as function argument

  2. 2

    Cannot pass an array of object to a function?

  3. 3

    cannot pass array in a function javascript

  4. 4

    Unable to pass an array as function argument in Javascript

  5. 5

    React .map is not a function but cannot pass array

  6. 6

    Cannot pass an array to a function from a multidimensional array

  7. 7

    Pass function with one argument to array.map using jsfuck

  8. 8

    array_map and pass 2 arguments to the mapped function - array_map(): Argument #3 should be an array

  9. 9

    passing array as javascript function argument from php

  10. 10

    javascript/typescript - force to pass object as function argument by eslint rule

  11. 11

    JavaScript throws: "is not a function" when I pass object as argument

  12. 12

    Javascript function of object as argument

  13. 13

    Return Object from map() function not Array

  14. 14

    How to pass array as an argument to a Ruby function from command line?

  15. 15

    How to pass an array as function argument?

  16. 16

    How to pass an array argument to a function

  17. 17

    Pass in part of an array as function argument

  18. 18

    Pass numpy array as function argument

  19. 19

    javascript - unused argument 'idtype' although called from a .map() function

  20. 20

    Map New array of object from array of object javascript

  21. 21

    Bash - pass argument from array

  22. 22

    Pass an object into an array in JavaScript

  23. 23

    Pass argument to flask from javascript

  24. 24

    Map array js - how to pass argument

  25. 25

    Cannot pass dictionary as an argument to a VBA function

  26. 26

    Why cannot pass function name as argument in this case?

  27. 27

    Cannot pass struct pointer as function argument in C

  28. 28

    Map object with array in javascript

  29. 29

    Pass a function with parameter as an argument to another function in JavaScript

HotTag

Archive