0

I am trying to save the value of parse array into global array. but global array showing me undefined

  dataUrl: string = "assets/data.csv";
  private data:[];
dataInit(){
    this.papa.parse(this.dataUrl, {
      download: true,
      complete: (result) => {
        // result.data.push(this.data);
        this.data = result.data
        // console.log(result.data, "inside parser");
        // console.log(this.data, "global array");
      }

    });
  }
  ngOnInit() {
    this.dataInit();
    console.log(this.data, "inside onInit");
}

Console

undefined "inside onInit"

5
  • Try to initialize data like private data:[] = []; Commented Nov 1, 2019 at 5:47
  • in this case, the data show Array (0) Commented Nov 1, 2019 at 5:50
  • I think console.log(this.data, "inside onInit"); prints before your this.dataInit(); method completes the execution. Try to un-comment // console.log(result.data, "inside parser"); and check. Commented Nov 1, 2019 at 5:53
  • @hrdisback, yes exactly this is the case. How can I access the array value out the side dataInit function? Commented Nov 1, 2019 at 5:54
  • You can use timeout block, you can use promise and observables check this for ref. medium.com/mr-frontend-community/… Commented Nov 1, 2019 at 5:57

5 Answers 5

1

There are two reasons for that -

  1. You need to initilize the variable like this private data: Array<any>= [];
  2. You are binding the value into asyn method and consoling the value in synchronous way.
Sign up to request clarification or add additional context in comments.

1 Comment

you are right, but do we have any approach to get the value into global and reuse is when we need?
1

The data will be available inside complete callback. So console.log(this.data) over there.

Reason: complete is a callback method which works asynchronously.

dataUrl: string = "assets/data.csv";
data = [];
dataInit(){
    this.papa.parse(this.dataUrl, {
      download: true,
      complete: (result) => {
        // result.data.push(this.data);
        this.data = result.data            
        console.log(this.data);
      }

    });
  }
  ngOnInit() {
    this.dataInit();
}

2 Comments

Yes, console show the data inside the papa.parse() function.I need to push the parse array into global array to access data in other functions. Is there any other approach to push the csv data into global?
@FaisalJanjua, you can access this.data in any functions as it is already in your this variable.
0

Change the initialization of the data property to something like

private data = [];

Or

private data: Array<T> = []

Instead of T type your array accordingly

2 Comments

I tried both private data:[] = []; and private data = [] both show Array(0).
Are you pushing in the results contents in the array?
0

Put the console log inside the complete function of the async code.

Because the papa.parse download code is asynchronous, the console log will show the initial value of data because the results are not ready yet.

Comments

0

Because this.papa.parse function is asynchronous, you can't get value of data variable right after calling dataInit... better to do inside complete callback

dataUrl: string = "assets/data.csv";
private data:[];
dataInit() {
  this.papa.parse(this.dataUrl, {
    download: true,
    complete: (result) => {
      this.data = result.data
      this.toDo();
    }
  });
}

ngOnInit() {
  this.dataInit();
}

toDo(){
  console.log(this.data, "global array");
}

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.