I'm creating a child process to which I'm sending a serialized request. Request is parsed inside child process and some of its fields are needed to do mongo operations, but queries are not retrieving any values. Queries work fine with same values from unserialized request.Didn't help, but tried using:req.facilityId = mongoose.Types.ObjectId(req.facilityId), // loader.js const cp = require('child_process') exports.addExcel = async(req, res, next) => { req.objects.userId = req.objects.session.userId; req.objects.facilityId = req.params.facilityId; req.objects.file = req.file; const serializedObj = JSON.stringify(req.objects) try { let worker = cp.fork('./utils/excelHelper.js'); worker.send(serializedObj) } catch (error) { console.error(error) } } // childProcess.js const mongoose = require('mongoose'); const Facility = require('../models/facility'); (async () =>{ try { process.on('message', async function(serializedObj) { console.log('Object received in ExcelHelper:'); let req = JSON.parse(serializedObj); req.facilityId = mongoose.Types.ObjectId(req.facilityId), // findOne not working here, but working // if value was never serialized: let facility = await Facility.findOne({_id: req.facilityId}); doSomething(facility); }); } catch (e) { console.log(e); } })()
Submitted January 02, 2020 at 02:06PM by 81mv
No comments:
Post a Comment