Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Cómo usar papa-parse-angular2

Quiere usar papa-parse-angular2 para convertir CSV a JSON. No encontré ningún ejemplo, así que lo hago así.

aplicación.módulo.ts

 import {CSVService} from 'papa-parse-angular2'; @NgModule({ providers: [ CSVService, ...

xx.componente.ts

 constructor( private csvService: CSVService, ... private x1() { let file = ...; this.csvService.parse(file, { complete: function(results) { // take results.data } });

No hay problema de construcción. Pero cuando lo ejecuté, obtuve el siguiente error. ingrese la descripción de la imagen aquí

 Uncaught TypeError: Cannot read property 'length' of undefined at CSVHandler.guessHeaders (vendor.bundle.js:105749) at CSVHandler.formHeaders (vendor.bundle.js:105734) at CSVHandler.setHeaders (vendor.bundle.js:105761) at vendor.bundle.js:64297 at ZoneDelegate.invoke (vendor.bundle.js:137052) at Object.onInvoke (vendor.bundle.js:4532) at ZoneDelegate.invoke (vendor.bundle.js:137051) at Zone.run (vendor.bundle.js:136812) at NgZone.run (vendor.bundle.js:4401) at vendor.bundle.js:64293 at SafeSubscriber.schedulerFn [as _next] (vendor.bundle.js:4247) at SafeSubscriber.__tryOrUnsub (vendor.bundle.js:14620) at SafeSubscriber.next (vendor.bundle.js:14569) at Subscriber._next (vendor.bundle.js:14509) at Subscriber.next (vendor.bundle.js:14473) at EventEmitter.Subject.next (vendor.bundle.js:15308) at EventEmitter.emit (vendor.bundle.js:4221)

No tengo idea de cómo arreglarlo. ¿U otras bibliotecas para convertir CSV a JSON en angular 4? Agradezco cualquier ayuda.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Podrías usar ngx-papaparse .

Primero instale la biblioteca:

Para Angular 6 ( docs ):

npm install ngx-papaparse --save

También existe una versión para Angular 2/4 y Angular 5 .

Luego impórtelo a su módulo (no tiene que ser su AppModule):

 import { PapaParseModule } from 'ngx-papaparse'; @NgModule({ ... imports: [ ... PapaParseModule ] })

Analizando CSV:

 import { Component } from '@angular/core'; import { Papa } from 'ngx-papaparse'; @Component({ ... }) export class AppComponent { constructor(private papa: Papa) { let csvData = '"Hello","World!"'; this.papa.parse(csvData,{ complete: (result) => { console.log('Parsed: ', result); } }); } }

También puede analizar un archivo, en lugar de una cadena. Simplemente reemplace csvData con el archivo.

Descargo de responsabilidad: Creé la biblioteca.

over 4 years ago · Santiago Trujillo Report

0

Simplemente puede usar papaparser .

 import * as Papa from 'papaparse/papaparse.min.js';

Usar

 onUpload(file: File) { Papa.parse(file, { complete: function(results) { console.log("Finished:", results.data); } }); }

Sin embargo, una desventaja es que no obtendrá ninguna definición de tipo de papaparse

over 4 years ago · Santiago Trujillo Report

0

Instale papaparse y @types/papaparse . Esto habilitaría definiciones de tipo para IDE intellisense.

Entonces, podrías importar todo el módulo papaparse

 import * as Papa from 'papaparse'

o especificar las importaciones requeridas como

 import { parse } from 'papaparse'

Se recomienda este enfoque sobre los contenedores de terceros, ya que no se actualizan con tanta frecuencia o tienen un esfuerzo comunitario limitado detrás de ellos.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!