I am using worker thread in node js, below is my code in two files
worker.js
const mysql = require('mysql2');
const {
v1: uuidv1,
v4: uuidv4,
} = require('uuid');
const {parentPort, workerData} = require("worker_threads");
const dayjs = require('dayjs');
parentPort.postMessage(getRdvSlots(workerData.data));
async function getRdvSlots(data){
if(data.duration == 0 || data.address_id.trim() === "" || data.reasonId.trim() === "" || data.people_id.trim() === "" ){
return false;
}
let details = {};
let startDate = data.sel_date;
details["startDate"] = startDate;
let availableDates = await generateAvailableDates(data.people_id, data.address_id, data.reasonId, startDate);
let emptyAvailabilities = 0;
let slots = {};
for(let key in availableDates){
slots[key] = await getSlots(key, availableDates[key],data.people_id, data.duration);
}
details["slots"] = slots;
return JSON.parse(JSON.stringify(details));
}
And in index.js
app.get("/checkworker", async (req, res) => {
const worker = new Worker("./worker.js", {workerData: {data: req.query}});
worker.once("message", result => {
console.log(`${result}`);
res.send(result);
});
worker.on("error", error => {
console.log(error);
});
worker.on("exit", exitCode => {
console.log(`It exited with code ${exitCode}`);
})
});
In this I am getting error of
DataCloneError: # could not be cloned. at Object. \worker.js:10:12
which is at line parentPort.postMessage(getRdvSlots(workerData.data));
in worker.js
I am new to nodejs worker thread any help what is error in my code
I am using worker thread in node js, below is my code in two files
worker.js
const mysql = require('mysql2');
const {
v1: uuidv1,
v4: uuidv4,
} = require('uuid');
const {parentPort, workerData} = require("worker_threads");
const dayjs = require('dayjs');
parentPort.postMessage(getRdvSlots(workerData.data));
async function getRdvSlots(data){
if(data.duration == 0 || data.address_id.trim() === "" || data.reasonId.trim() === "" || data.people_id.trim() === "" ){
return false;
}
let details = {};
let startDate = data.sel_date;
details["startDate"] = startDate;
let availableDates = await generateAvailableDates(data.people_id, data.address_id, data.reasonId, startDate);
let emptyAvailabilities = 0;
let slots = {};
for(let key in availableDates){
slots[key] = await getSlots(key, availableDates[key],data.people_id, data.duration);
}
details["slots"] = slots;
return JSON.parse(JSON.stringify(details));
}
And in index.js
app.get("/checkworker", async (req, res) => {
const worker = new Worker("./worker.js", {workerData: {data: req.query}});
worker.once("message", result => {
console.log(`${result}`);
res.send(result);
});
worker.on("error", error => {
console.log(error);
});
worker.on("exit", exitCode => {
console.log(`It exited with code ${exitCode}`);
})
});
In this I am getting error of
DataCloneError: # could not be cloned. at Object. \worker.js:10:12
which is at line parentPort.postMessage(getRdvSlots(workerData.data));
in worker.js
I am new to nodejs worker thread any help what is error in my code
Share Improve this question asked Jan 7, 2022 at 13:31 JayJay 8413 gold badges19 silver badges51 bronze badges1 Answer
Reset to default 4I found this question b/c I had the same issue. I just fixed it :) The object I was trying to post to the other thread had a property an async method was setting. I did not add await to the line :)
I think this will fix your issue:
parentPort.postMessage(await getRdvSlots(workerData.data));