0

I have this array called fieldlist which is coming from the below http.get request in angular 2 component. I have declared it like this .

                   fieldlist: string[] = [];

then I assigned its value using iteration of json resposne.

               this.http.get(getform_endpoint,requestOptions).map((res: 
                                        Response) => res.json()).subscribe(
                          res => { 

                   this.FormData = res.schema;


                     res.fields.forEach(element => {
                         this.fieldlist.push(element);
                     });


          });  

Now in another function like this

         create_hidden_qp() {


            let elementsnamevalue = this.fieldlist.join();
            console.log("hello", this.fieldlist.join());

      }

when I convert it to string using this lines it gives me blank "" response

but when I print it like this

                 console.log("hello", this.fieldlist);

I am able to see the perfect array like this.

           hello[] 0 :"userroleid" 1: "ruletype" 2: "employeeid"

So what I am missing?

A) wrong declaration? b) wrong assigment? c) wrong access to array elements?

12
  • I have tried this.fieldlist[0], this.fieldlist[1] etc, but its undefined. I have tried this.fieldlist.tostring() but its also gives me blank response. What I am missing? Commented Jan 22, 2018 at 15:17
  • 6
    option d), wrong timing. You probably try to access the fieldlist before the response has completed. Remember that JavaScript, and with it TypeScript are async languages Commented Jan 22, 2018 at 15:20
  • The join function needs an argument to join an array. Try using something like join(' ') Commented Jan 22, 2018 at 15:21
  • @PierreDuc Yup that may be a new case but then why I am able to read when printing it raw like this? console.log("hello", this.fieldlist); Commented Jan 22, 2018 at 15:21
  • 1
    @Jaydeep because the console holds a reference and will actually update when the data does come through Commented Jan 22, 2018 at 15:24

1 Answer 1

1

You should call the create_hidden_qp after your request has finished:

this.http.get(getform_endpoint,requestOptions).map(r => r.json()).subscribe(res => { 
  this.FormData = res.schema;

  res.fields.forEach(element => {
    this.fieldlist.push(element);
  });

  this.create_hidden_qp();
});  
Sign up to request clarification or add additional context in comments.

4 Comments

This has make sure that I got the value when needed. Amazing answer. Thank you all.
But I am still curios to know that why after USING .join I am not able to get value but when I print it as raw, got it. Is it because I just passed by value to console as opposed to passing by reference when I am able to see it.
@Jaydeep Yes, that's a thing of the console, you just log a reference, and once you actually open the array in the console, your request has already finished, and it will show the filled array :)
Ok so lesson learned is while using async language, Don't delay assignment and conversion as it my defer.

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.