0

How can i get this number from productCount.value (screenshot)?

Trying productCount.value.array, but i will get again proxy - [[target]] - array

Edit 1: If i use productCount.value.array[0] i will get error Invalid value used as weak map key

11

Jessika
  • 31
  • 8
  • 2
    Have you tried `productCount[0]`? – shaedrich Sep 26 '22 at 09:16
  • 1
    Please visit the [help], take the [tour] to see what and [ask]. If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [\[<>\]](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Sep 26 '22 at 09:19
  • 1
    @shaedrich - The OP claims the screenshot is a log of `productCount.value`, not `productCount`. – T.J. Crowder Sep 26 '22 at 09:19
  • 1
    Please post code, error messages, console output, markup, data structures, and other textual information **as text**, not just as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247 – T.J. Crowder Sep 26 '22 at 09:21
  • 1
    **If** that output is really from logging `productCount.value`, and `productCount.value.array` doesn't work, then the proxy doesn't let you have access to `productCount.value.array`. You can't directly access the [[Target]] of a Proxy, that's part of the point of them. So although the target has an `array` property, the proxy doesn't have to grant you access to it. – T.J. Crowder Sep 26 '22 at 09:21
  • Have you by chance overwritten the `Proxy`'s `get()` method? – shaedrich Sep 26 '22 at 09:50

2 Answers2

2

Trying productCount.value.array, but i will get again proxy - [[target]] - array

That's not necessarily a problem. If the proxies (apparently there are at least two involved) allow you to access that array and its element 0, and you got the screenshot you showed from console.log(productCount.value), you can do so like this:

const elementZeroValue = productCount.value.array[0];

Basically, you pretend the proxy isn't there, since it's a facade on the target. (A facade that may well limit or modify what you see.)

But that's only if the proxies involved allow that access. You can't directly access the [[Target]] of a Proxy, that's part of the point of them.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

A Proxy in JavaScript stores its values internally within [[target]] which, as a property, is inaccessable. However, you can use the proxy in the same manner as you would use that object (or in your case array) normally because—as long as you hasn't tampered with that mechanism by overwriting its get() method—takes care of that, so productCount.value.array[0] should suffice.

If you have overwritten the Proxy's get method, we would need to know about it to provide the proper answer.

shaedrich
  • 5,457
  • 3
  • 26
  • 42