http icon indicating copy to clipboard operation
http copied to clipboard

Error: The object can not be cloned

Open enricodeleo opened this issue 4 years ago • 7 comments

Describe the bug In some context, while sending requests containing payload as objects I get the error The object can not be cloned.

Desktop (please complete the following information):

  • OS: Ionic on iOS

Smartphone (please complete the following information):

  • Device: iPhone 12
  • OS: 14

Additional context As far as I can understand the JS Object is transferred to the native side of the plugin as is and then serialized there, causing issues in case the object has references to other variables.

enricodeleo avatar Jun 18 '21 11:06 enricodeleo

Can you provide a reproduction repo on Github?

thomasvidas avatar Jun 25 '21 21:06 thomasvidas

Take a look at this document: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

This error is indicative of passing something over a bridge that can't be properly serialized (usually it's the renderer<->webworker bridge, but in this case it's between the renderer<->native). The most common form of this issue is trying to include dom elements in messages that leave the renderer context.

emily-curry avatar Aug 27 '21 05:08 emily-curry

I am facing the same issue where i am trying to send file object in Form data,

DataCloneError: The object can not be cloned

rightmentor avatar Oct 08 '21 14:10 rightmentor

I see the same issue here, basically my case is I am trying to capture an image, but the image is in memory only, and then we use javascript File to create a file and append it to FormData, but I see the issue DataCloneError: The object can not be cloned

maleo avatar Dec 02 '21 04:12 maleo

@rightmentor I'm also encountering this issue in a case where I'm sending a file object in FormData. I'll follow-up if I figure out how to address.

nonsenseless avatar Dec 08 '21 00:12 nonsenseless

Facing the same issue as @maleo i.e. facing this error when trying to post image. If anybody finds a solution please update this thread.

jindalrj avatar Jan 27 '22 09:01 jindalrj

In my case I was using FormData which is not serializable, and thus this error was being emitted. To handle it, I converted to a plain JavaScript object via below code.

const convertToSerializableObject = (data: FormData) => {
    const object = {};
    data.forEach(function(value, key){
        object[key] = value;
    });
    return object;
}

That alone did not fix the problem as this library does not support multipart/form-data. To send files over the network, I passed file references in the payload and when contentType is multipart/form-data, resolved them within the library, see code here

jindalrj avatar Jan 31 '22 05:01 jindalrj