Skip to main content

When I am trying to deploy to Org, I am getting error saying that 'structuredClone is not defined'.

 

However the 'Authorize an org' is successful.

 

I am using node-v20.10.0 version.

 

How to resolve this issue?

22 respuestas
  1. 25 jul 2024, 09:42

    The JSON.parse(JSON.stringify(object)); mentioned here https://salesforce.stackexchange.com/questions/371905/structuredclone-is-not-a-function-lwc is not advisable. This does not "technically" deep clone some more complex data types. https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521

     

    The short answer is to write you're own clone function or use a library like Lodash deepClone. 

     

    Example Clone. 

     

    /**

    * Custom function to deep clone objects. Support for multiple types.

    * Supports, Date, Map, RegExp, Function, ArrayType, Array, Object, primitive types. Also dupes property flags and descriptors.

    *

    * This is designed to replace the common "hack" of JSON.parse(JSON.stringify(myObj));

    * Unfortunately LWC doesn't support the native structuredClone();

    * https://developer.salesforce.com/docs/component-library/tools/locker-service-viewer - LWC API support

    * https://developer.mozilla.org/en-US/docs/Web/API/structuredClone - MDN for structuredClone

    * https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521 - Great article on deep cloning.

    *

    * Usage: LWCHelpers.cloneDeep(myObject);

    * @param {Iterable} value - The "object" to clone.

    * @param {WeakMap} hash - Internal storage for cloning JS Map Types

    * @returns {Iterable} - Clone of the original "object"

    */

    static cloneDeep(value, hash = new WeakMap()) {

    if (Object(value) !== value) {

    // Handle primitive types and special cases

    if (typeof value === 'number' && isNaN(value)) return NaN;

    if (value === Infinity) return Infinity;

    if (value === -Infinity) return -Infinity;

    return value; // Return primitive types directly

    }

    if (typeof value === 'symbol') return Symbol(value.description); // Handle symbols

    if (value instanceof Function) return value.bind({}); // Handle functions

    if (hash.has(value)) return hash.get(value); // Handle cyclic references

    if (value instanceof Date) return new Date(value); // Handle Date

    if (value instanceof RegExp) return new RegExp(value); // Handle RegExp

    if (value instanceof Map) { // Handle Map

    const result = new Map();

    hash.set(value, result);

    value.forEach((val, key) => result.set(key, LwcHelpers.cloneDeep(val, hash)));

    return result;

    }

    if (value instanceof Set) { // Handle Set

    const result = new Set();

    hash.set(value, result);

    value.forEach(val => result.add(LwcHelpers.cloneDeep(val, hash)));

    return result;

    }

    // Handle TypedArray

    if (ArrayBuffer.isView(value)) {

    return new value.constructor(value.buffer.slice(0));

    }

    // Handle Array and Object

    const result = Array.isArray(value) ? [] : {};

    hash.set(value, result);

    const descriptors = Object.getOwnPropertyDescriptors(value);

    Object.keys(descriptors).forEach(key => {

    descriptors[key].value = LwcHelpers.cloneDeep(descriptors[key].value, hash);

    Object.defineProperty(result, key, descriptors[key]);

    });

    return result;

    }

0/9000