0

I tried map elements to [] for angular. But if I checked length of objects there is always 0..

var objects = [];

this.get().subscribe(
  response => {
    for (var i = 0; i < response.length; i++) {
      objects.push(response[i]);
    }
  }
);
console.log(objects.length);

enter image description here

screen if I console.log(objects)

What I am doing wrong?

16
  • 3
    Why don't you just do objects = response? Also, where is objects defined ? Commented Nov 14, 2018 at 11:49
  • I cant - objects will be empty - idk why is that Commented Nov 14, 2018 at 11:50
  • What? The screenshot says length: 6. Where do you get that it's 0? Commented Nov 14, 2018 at 11:50
  • 1
    Please provide some stackblitz, currently it's unclear how you come to this situation, and what do you need. (for Angular, the best way to create a minimal reproducible example is currently to use StackBlitz) Commented Nov 14, 2018 at 11:51
  • 2
    The console.log in your above example will print 0 because your closure hasn't been executed yet. Commented Nov 14, 2018 at 11:56

4 Answers 4

2

This is due to the asynchronous nature of execution of the code. The subscribe method accepts a callback, which is the arrow function that you have written with the parameter named response.

So, the callback will not be executed immediately but will be done after a while. But, since JS is asynchronus, it wouldn't wait for the callback to get executed and will move on to the next line of the code where the variable will still be an empty array.

As suggested in the other answers, you could put the console log within the callback function to log the expected value.

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

Comments

1

You are doing console.log before getting response in subscribe. Just move console.log inside subscribe.

var objects = [];

this.get().subscribe(
  response => {
    for (var i = 0; i < response.length; i++) {
      objects.push(response[i]);
    }
    console.log(objects.length);
  }
);

1 Comment

Yes but i have to return "objects"
0

You are logging something that's set later so the preview shows an empty array but you can expand it.

The log will show the item as it is right now; not as it is when you log it.

var arr = [];
console.log(arr);//shows [] but when you expand it it shows items
arr.push({name:'john'});

If you want to see an object's values at the time you are logging the object and plan to mutate it after the log then you can do the following:

console.log(JSON.stringify(object,undefined,2);

Your objects.push(response[i]); statement is in a callback and that callback is probably called after your console.log(objects.length); you can proof that by adding another log:

var objects = [];

this.get().subscribe(
  response => {
    console.log('second log')
    for (var i = 0; i < response.length; i++) {
      objects.push(response[i]);
    }
  }
);
console.log("first log",objects.length);

If you have a value that resolves once at a later time then you could use Promises but it looks like you subscribe to an event that can resolve multiple times so your response will only be available in the callback.

2 Comments

Okay. My "objects" must be array, because it will be used in ngFor. In another file I check objects.length and I little parse this list. So this must be synchronous or idk..
@KamilKamil I'm not familiar using Angular observables but maybe this answer will help.
-1

Seems that subscribe code is async, so you can wrap this up on a promise. result is the objects you want

const promise = new Promise((resolve, reject) => {
  var objects = [];
  this.get().subscribe(response => {
    for (var i = 0; i < response.length; i++) {
      objects.push(response[i]);
    }
    resolve(objects);
  });
});

promise.then(result => console.log(result));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.