0

I have a page to uploaded excel

    <div class="form-group">
    <label>Select file to upload.</label>
    <input type="file" class="form-control" (change)="onFileChange($event);">
    </div>
    <button type="button" (click)="Upload()" class="btn btn-success pull-right"><i class="fa fa-save fa-fw"></i> Upload File</button>

Below is my upload() function and onFileChange() in component.ts

 onFileChange(event) {
   if (event.target.files.length > 0) {
    this.file = event.target.files[0];
   }
 }

 Upload() {
      let fileReader = new FileReader();
    fileReader.onload = (e) => {
        this.arrayBuffer = fileReader.result;
        var data = new Uint8Array(this.arrayBuffer);
        var arr = new Array();
        for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
        var bstr = arr.join("");
        var workbook = XLSX.read(bstr, {type:"binary"});
        var first_sheet_name = workbook.SheetNames[0];
        var worksheet = workbook.Sheets[first_sheet_name];
        this.exceljsondata = XLSX.utils.sheet_to_json(worksheet,{raw:true, defval:""});

       console.log(this.exceljsondata);
        this.providerservice.importexcel(this.exceljsondata).subscribe(data=>{
        })
    }
    fileReader.readAsArrayBuffer(this.file);
}

The output for above console.log(this.exceljsondata) is as below

  0: {Your First Name: "", Your Last Name: "", Agency/Practice Name: "2-1-1 Big Bend", Agency's Location - Address: "", Agency's Location - Address (House Number): "", …}
  1: {Your First Name: "Melanie", Your Last Name: "Rosemberg", Agency/Practice Name: "360º Therapy", Agency's Location - Address: "1380 NE Miami Gardens Dr Suite 242", Agency's Location - Address (House Number): "1380", …}

This is my provider service where I am posting the excel json data to node js

   public importexcel(providrdata):Observable<any> {
        return this.http.post(this.baseUrl+"provider/importexcel", JSON.stringify(providrdata), httpOptions).pipe(map((res)=> res));
    }

This is my function in node js. I am trying to insert this json data into database

    router.post('/importexcel',(req, res) => {
       console.log(req.body)
    })

The output of console.log here in nodejs is below

[ { 'Your First Name': '',
    'Your Last Name': '',
    'Agency/Practice Name': '2-1-1 Big Bend',
    'Agency\'s Location - Address': '',
    'Agency\'s Location - Address (House Number)': '',
    'Agency\'s Location - Address (Street)': '',
    'Agency\'s Location - City': '',
    'Agency\'s Location - State': '',
    'Agency\'s Location - Postal Code': '',
    'Practice/Agency Location: County': '',
    'Phone Number': '211 and (850) 617-6333',
    Email: '',
    Website: 'http://www.211bigbend.org',
    'Which category best describes your private practice or agency?* - Selected Choice': 'Hotline',
    'Which category best describes your private practice or agency?* - Other - Text': '',
    'Please select any additional descriptors which fit your private practice or agency. - Selected Choice': '',
    'Please select any additional descriptors which fit your private practice or agency. - Other - Text': '',
    Specialty:
     'Crisis Counseling,Maternal Mental Health,Referrals,Resource,Suicide Prevention',
    'Insurance - Selected Choice': 'No charge for services',
    'Insurance - Other - Text': '',
    Serves:
     'Individual,Adolescents,Caregivers,Children,Couples,Family,Geriatrics,Groups,Women',
    'Areas Served':
     'Franklin,Gadsden,Jefferson,Leon,Liberty,Madison,Taylor,Wakulla',
    'Days/Hours of Operation - Selected Choice': 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday',
    'Days/Hours of Operation - Monday - Text': '',
    'Days/Hours of Operation - Tuesday - Text': '',
    'Days/Hours of Operation - Wednesday - Text': '',
    'Days/Hours of Operation - Thursday - Text': '',
    'Days/Hours of Operation - Friday - Text': '',
    'Days/Hours of Operation - Saturday - Text': '',
    'Days/Hours of Operation - Sunday - Text': '',
    'Do you provide telehealth services? - Selected Choice': '',
    'Do you provide telehealth services? - Yes (other) - Text': '',
    'Accepting New Clients?': 'Yes',
    'Additional Information/Description': '' },
  { 'Your First Name': 'Melanie',
    'Your Last Name': 'Rosemberg',
    'Agency/Practice Name': '360º Therapy',
    'Agency\'s Location - Address': '1380 NE Miami Gardens Dr Suite 242',
    'Agency\'s Location - Address (House Number)': '1380',
    'Agency\'s Location - Address (Street)': 'NE Miami Gardens Dr Suite 242',
    'Agency\'s Location - City': 'North Miami Beach',
    'Agency\'s Location - State': 'Florida',
    'Agency\'s Location - Postal Code': 33179,
    'Practice/Agency Location: County': 'Miami-Dade',
    'Phone Number': 3053966009,
    Email: '[email protected]',
    Website: 'www.melanierosemberg.com',
    'Which category best describes your private practice or agency?* - Selected Choice': 'Licensed Mental Health Counselor',
    'Which category best describes your private practice or agency?* - Other - Text': '',
    'Please select any additional descriptors which fit your private practice or agency. - Selected Choice': 'Support Group',
    'Please select any additional descriptors which fit your private practice or agency. - Other - Text': '',
    Specialty:
     'Anxiety,Counseling,Depression,Maternal Mental Health,Psychotherapy',
    'Insurance - Selected Choice': 'Self pay by check or cash,Sliding scale fees available',
    'Insurance - Other - Text': '',
    Serves: 'Individual,Adolescents,Caregivers',
    'Areas Served': 'Broward,Miami-Dade',
    'Days/Hours of Operation - Selected Choice': 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
    'Days/Hours of Operation - Monday - Text': 43717,
    'Days/Hours of Operation - Tuesday - Text': 43717,
    'Days/Hours of Operation - Wednesday - Text': 43717,
    'Days/Hours of Operation - Thursday - Text': 43717,
    'Days/Hours of Operation - Friday - Text': 43712,
    'Days/Hours of Operation - Saturday - Text': 43680,
    'Days/Hours of Operation - Sunday - Text': '',
    'Do you provide telehealth services? - Selected Choice': 'Yes (via video),Yes (via phone)',
    'Do you provide telehealth services? - Yes (other) - Text': '',
    'Accepting New Clients?': 'Yes',
    'Additional Information/Description': '' } ]

How do I get value in 'Your First Name', 'Your Last Name', 'Agency/Practice Name' , etc as they have space in between. I can't call it as below

 router.post('/importexcel',(req, res) => {
   for(lnt of req.body){
       var firstname = lnt.'Your First Name';
       var lastname = lnt.'Your Last Name';
   }
})

How do I get these variable. As i need to insert them into database. Please help

1 Answer 1

0

You can use JavaScript bracket notation to access the desired properties on these objects.

For example:

let obj = {};

// Set properties using bracket notation
obj['First Name'] = 'Jim';
obj['Last Name'] = 'Smith';

// We can still use .dot notation if we wish
obj.Age = 42;

// Access properties using bracket notation
console.log('Accessing object properties using [bracket] notation:');
console.log('First name: ' + obj['First Name']);
console.log('Last name: ' + obj['Last Name']);
console.log('Age: ' + obj['Age']);


// Enumerate using Object.entries:
console.log('\nEnumerating object entries:');
for (let [key, value] of Object.entries(obj)) {
    console.log(`${key}: ${value}`);
}

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

Comments

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.