0

Below, you can see the output from these two logs. One is of the object itself, the other is of one of it's properties. Seems like a weird behaviour, where the property is set and visible in the first log, but undefiened when accessing it directly.

  1. The string returned via Get request from a text file (blob).

Important Note: The strings are a bit large (300kb)

This is what the assignment of the string looks like:

await this.http.get(assetPath, {responseType: 'text'}).subscribe((content) => {
            /// Conent is the string representing the text in the file
            dynamicAsset.original = content;
            this.findAndChangeAsset(dynamicAsset);
}
  1. Trying to console.log the object returns the object with the property 'original' set properly:

Object itself

  1. Trying to access the property itself and log it, shows it's undefined

Object propery

The console logs whithin findAndChangeAsset. As you can see, they are one line after the other. I can even change the order and it won't work. Crazy!

enter image description here

Things I tried with no success:

  1. JSON.stringify(obj) / JSON.parse(obj)
  2. Accessing foundAsset['original']
  3. Concat empty strings
Itzik Gili
  • 324
  • 3
  • 16
  • 1
    What have you white-d out there? It might be blocking some of the display that we need to see. – kelsny Oct 24 '22 at 20:50
  • Shouldn't `responseType` be `json` or `application/json` or something similar? Just guessing. It looks like json data that is returned, but because of `responseType: 'text'` the returned format is weird. – Bqardi Oct 24 '22 at 21:07
  • re: whiting a url, pretty insignificant. – Itzik Gili Oct 24 '22 at 21:08
  • re: application/json, I want to treat it a text. It might not be a valid json – Itzik Gili Oct 24 '22 at 21:09
  • what is `foundAsset`? same as `dynamicAsset`? or could it be a problem of accessing object before it was fetched? – IT goldman Oct 24 '22 at 21:21
  • same and actually called fron within the callback method – Itzik Gili Oct 24 '22 at 21:29
  • also, I can even change the order and it will behave the same. this is soooo weird – Itzik Gili Oct 24 '22 at 21:34
  • 3
    If you look at the one-line representation of the object, you'll see that at the moment in time when it was logged (_not_ the moment in time you clicked the arrow to expand it!) there was only `path` and `type` in the object and none of the other properties. So it seems like you attempt to access the `original` property before it even gets written. – CherryDT Oct 24 '22 at 21:37
  • 2
    Google chrome dev tools lazily evaluates object references, so even though you `console.log` spatially _before_ you try to access that property, in fact it might have been added _later_. To be absolutely sure `console.log(Object.keys(obj))` – zerkms Oct 24 '22 at 21:39
  • will try to print the keys and report back. maybe it's an asynchronous issue – Itzik Gili Oct 24 '22 at 21:41
  • Where do you define/initialize `dynamicAsset`? If it's declared outside of the handler function and that function gets called multiple times (in case of of multiple `get()` calls), then each invocation may overwrite the previous value that `dynamicAsset` stores. – Peter B Oct 24 '22 at 21:48
  • ok, indeed it was a synchronisation problem. I'll post a solution later today. – Itzik Gili Oct 25 '22 at 07:46
  • A good indication of the properties that loaded asynchronously is what @CherryDT and zerkms refer to above – Itzik Gili Oct 25 '22 at 07:49

0 Answers0