Error : Cannot read property 'cloneNode' of undefined

12,564
var checkB=document.createElement("input").setAttribute('type', 'checkbox', 'name', 'delete');

should be

var checkB=document.createElement("input");
checkB.setAttribute('type', 'checkbox');
checkB.setAttribute('name', 'delete');

.setAttribute does not return the element, and it only takes two arguments.

Share:
12,564
EL MASLOUHI HAMZA
Author by

EL MASLOUHI HAMZA

Updated on June 04, 2022

Comments

  • EL MASLOUHI HAMZA
    EL MASLOUHI HAMZA almost 2 years

    I'm trying to add a listener to my Database using Firebase.

    const dbRef=firebase.database().ref().child('messages');
    dbRef.on('child_added', MS.bringData);
    

    When something is added, I execute bringData which creates a new object called message from the snap object.

    export const bringData = (snap) => {
      const message1=new Message(snap.val().objet, snap.val().message, snap.val().etat, snap.val().key);
      message1.creerLigne();
    }
    

    Then I call creerLigne() from the class Message, to create the new line and add it to tbody

    creerLigne() {
         const arg=[this._objet, this._message, this._etat, this._id];
         const row=document.createElement("tr");
         row.setAttribute('id', this._id);
         const data=document.createElement("td");
         var checkB=document.createElement("input").setAttribute('type', 'checkbox', 'name', 'delete');
    
         var i=0;
         while(i<arg.length-1) {
             let data1=data.cloneNode();
             let text=document.createTextNode(arg[i]);
             data1.appendChild(text);
             row.appendChild(data1);
             i++;
         }
    
         data1.appendChild(text);
         row.appendChild(data1);
         row.appendChild(checkB.cloneNode());
         document.body.appendChild('row');
    }
    

    but unfortunately I'm getting the following error:

    FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'cloneNode' of undefined
        at Message.creerLigne