Q:

Object overwritten in firebase

Data sending from google sheet:

Script using to send data to firebase from google sheet.
    function writeData() { 
var ss = SpreadsheetApp.openById("####");
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};

for(var i = 1; i < data.length; i++) {
var department = data[i][0];
var year = data[i][1];
var course = data[i][2];

dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}

dataToImport[department][year][course][i] = {
            course: data[i][3],
            dateAdded: data[i][4],
            fileSize: data[i][5],
            fileType:data[i][6],
            downloadLink: data[i][7],  
          };

}
var firebaseUrl = "https:url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
base.setData("",dataToImport);  

}

Data that is sent: 

Q: My questions that since i had parent electrical and its two childs that were 1 and 2 and each child had a course dsp and aes respectively but only 1 child is send that is the second one.
Why first child is not sent to firebase ? 

A:

This code replacing previous entry with new entry. Replace this -
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}

with this -
dataToImport[department] = dataToImport[department] || {};
dataToImport[department][year] = dataToImport[department][year] || {}
dataToImport[department][year][course] = dataToImport[department][year][course] || {}

