Экспорт CSV в Ionic Framework и Parse-Server - PullRequest
0 голосов
/ 21 мая 2018

В JSbin я создал этот маленький скрипт, который показывает, что CSV-файл в консоли отлично находит.

//jshint esnext:true    
Parse.initialize("MyAppID", "JavascriptKey");

Parse.serverURL = "https://parseapi.back4app.com/";

 function objectToCSV(objectArray){
    const replacer = (key, value) => value === null ? 'NaN' : value;
    const headers = Object.keys(objectArray[0]);
    var csv = objectArray.map(row => headers.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','));
    csv.unshift(headers.join(','));
    return csv.join('\r\n'); 
}

//Creates an Array of Objects
var SurveyData = Parse.Object.extend("SurveyData");
var q = new Parse.Query("SurveyData");
dataArray = [];
q.limit(2);
q.find().then(function(results){
  for (var i = 0; i < results.length; i++){
    var data = results[i];

    //Pushes objects into An Array 
    dataArray.push(data.attributes);
  }
  //Returns Array of Objects
  console.log(objectToCSV(data));
});

Однако, когда я пытаюсь приблизиться к чему-то похожему на странице в моем приложении Ionic, например:this

export class TablesPage{
  dataArray = [];
  //Query is in a Ionic Provider
  constructor(parseProvider:ParseServerProvider){
  this.convertQuery();
  //convertToCSV is the same function as objectToCSV from JSBIN
  var items = this.convertToCSV(this.dataArray);
  }  
public convertQuery(){

//Returns the query then displays those "result" by pushing into surveyPoints object
return this.parseProvider.getQuery(offset=0, limit=100, 'SurveyData').then((result) => {
  for (let i = 0; i < result.length; i++) {
    let object = result[i];
    this.dataArray.push(object.attributes);
  }
  //console.log(dataArray);
  console.log(this.dataArray);

}, (error) => {
  console.log(error);
});
//console.log(dataArray);
}
}

Я получаю эту ошибку

core.js:1448 ERROR Error: Uncaught (in promise): TypeError: Cannot convert 
undefined or null to object
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at TablesPage.webpackJsonp.474.TablesPage.convertToCSV (tables.ts:103)
at new TablesPage (tables.ts:46)
at createClass (core.js:12449)
at createDirectiveInstance (core.js:12284)
at createViewNodes (core.js:13742)
at createRootView (core.js:13631)
at callWithDebugContext (core.js:15056)
at Object.debugCreateRootView [as createRootView] (core.js:14339)
at ComponentFactory_.create (core.js:11236)
at Function.keys (<anonymous>)
at TablesPage.webpackJsonp.474.TablesPage.convertToCSV (tables.ts:103)
at new TablesPage (tables.ts:46)
at createClass (core.js:12449)
at createDirectiveInstance (core.js:12284)
at createViewNodes (core.js:13742)
at createRootView (core.js:13631)
at callWithDebugContext (core.js:15056)
at Object.debugCreateRootView [as createRootView] (core.js:14339)
at ComponentFactory_.create (core.js:11236)
at c (polyfills.js:3)
at Object.reject (polyfills.js:3)
at NavControllerBase._fireError (nav-controller-base.js:223)
at NavControllerBase._failed (nav-controller-base.js:216)
at nav-controller-base.js:263
at t.invoke (polyfills.js:3)
at Object.onInvoke (core.js:4749)
at t.invoke (polyfills.js:3)
at r.run (polyfills.js:3)
at polyfills.js:3

Однако моя консоль все еще показывает, что объекты извлекаются.В чем проблема?Я просто хочу экспортировать информацию с моего сервера разбора в CSV-файл для клиентов.Спасибо!

...