最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs - DataCloneError: function () { [native code] } could not be cloned - Stack Overflow

programmeradmin3浏览0评论

I'm trying to spawn threads (using the new Nodejs module 'worker_threads') and pass to each of them a complex object which is the 'page' object of a Puppeteer browser.newPage() instance. I tried both using workerData and MessageChannels - which, from docs:

port.postMessage: Sends a JavaScript value to the receiving side of this channel. value will be transferred in a way which is compatible with the HTML structured clone algorithm. In particular, it may contain circular references and objects like typed arrays that the JSON API is not able to stringify.

but I always get the error:

(node:7133) UnhandledPromiseRejectionWarning: DataCloneError: function () { [native code] } could not be cloned. at Worker.postMessage (internal/worker.js:378:23)

I also tried to stringify it and parse it but the page object also contains functions which I couldn't get to evaluate from the threads context (I also would like to avoid using eval()).

The question is: how do I pass a complex object like Puppeteer browser.newPage() instance to the threads spawned with worker_threads in Nodejs?

I'm trying to spawn threads (using the new Nodejs module 'worker_threads') and pass to each of them a complex object which is the 'page' object of a Puppeteer browser.newPage() instance. I tried both using workerData and MessageChannels - which, from docs:

port.postMessage: Sends a JavaScript value to the receiving side of this channel. value will be transferred in a way which is compatible with the HTML structured clone algorithm. In particular, it may contain circular references and objects like typed arrays that the JSON API is not able to stringify.

but I always get the error:

(node:7133) UnhandledPromiseRejectionWarning: DataCloneError: function () { [native code] } could not be cloned. at Worker.postMessage (internal/worker.js:378:23)

I also tried to stringify it and parse it but the page object also contains functions which I couldn't get to evaluate from the threads context (I also would like to avoid using eval()).

The question is: how do I pass a complex object like Puppeteer browser.newPage() instance to the threads spawned with worker_threads in Nodejs?

Share Improve this question asked Nov 28, 2018 at 11:40 cub33cub33 6271 gold badge6 silver badges12 bronze badges 2
  • Very accurate question, I am also looking for the same solution. – MechaCode Commented Jun 18, 2020 at 5:01
  • Just had the same issue today, and it can't be done. The docs are rubbish on this and doesn't say anything about what the workerData will accept. – volume one Commented Jan 31, 2024 at 0:37
Add a comment  | 

3 Answers 3

Reset to default 3

dethSwathch is right about that: You can't. Message passing is only allowed for some few native data types, including Object (and Date and of course TypedArrays). That is why you lose all type information and functions. However, you can certainly use the "naked" object to just fill a new Page instance (or any other custom object).

This DataCloneError seems to be a limitation of the past. At least I couldn't reproduce it. The object should be copied automatically without any serialization.

Believe the answer is basically 'nope'.

Can you change what you're passing such that you can 'reconstitute' it on the other side?

The model is akin to making a call to another server- you have to encode everything you need and then reform it on the other side.

I have the same issue. I solved it by creating new class instance in each trad, so that I can pass only primitive arguments which require my constructor

发布评论

评论列表(0)

  1. 暂无评论