2

Because Lightning Web Components so often wrap objects in proxy objects, we long ago created a deProxyObj in order to be able to log to the console while debugging.

const deProxyObj = (proxyObj) => {
    return JSON.parse(JSON.stringify(proxyObj));
};

It's simple and allowed us to investigate proxy object contents while building the component. But a few months ago (don't know exactly when) this started returning a proxy object back, and I don't understand why. Finally decided to try to fix it, and did this:

const deProxyObj = (proxyObj) => {
    const str = JSON.stringify(proxyObj);
    console.log('str', str);
    const res = JSON.parse(str);
    console.log("res", res);
    return res;
};

When this gets executed, the log looks something like this (for data returned by Apex):

str {"0":{"Id":"a048K000000oRbnQAE","Address__c":"789 Third ...
res Proxy {}

So I'm successfully getting the proxy's contents as a string but then when I parse that string instead of giving me a standard object, it's wraping the object in a proxy object again, defeating the entire purpose.

So first, why did this used to work and now doesn't. And second, how can I extract the proxy object so I can investigate its contents for debugging purposes?

4
  • Have you recently enabled LWS? (It is now auto-enabled on scratch orgs.) Commented Dec 9, 2022 at 15:08
  • @PhilW, this is in a scratch org, and it did have LWS enabled, but I disabled it in settings and reloading the page had no change in results. Commented Dec 9, 2022 at 15:15
  • We found that switching required total session restart. Make sure you don't have the same session active and try again. Commented Dec 9, 2022 at 15:20
  • 1
    @PhilW, I added "lockerServiceNext": false to project-scratch-def.json, spun a new scratch org and that fixed it. Please post an answer so I can give you the credit. Commented Dec 9, 2022 at 15:29

1 Answer 1

3

LWS, the Lightning Web Security replacement for the Aura Locker Service is now enabled by default on scratch orgs. It appears that LWS proxifies the JSON.parse response, unlike the Locker Service.

You can explicitly turn off LWS on the org but this only takes effect for new sessions.

You can ensure new scratch orgs use the Locker Service by adding "lockerServiceNext": false to your scratch def JSON.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.