0

Here I am using fork method to do some calculation in node js. I have followed by the answer in link.It worked as expected and had started to do calculation.

Here how can I get the details from the mongodb in child file.I am using mongoose for mongodb. My sample code is below

main.js

    var childprocess = require('child_process');
    var child = childprocess .fork(__dirname + '/childpage');
    child.on('message', function(m) {
   // Receive results from child process
    console.log('received: ' + m);
   });

childpage

   var User = require('../models/User');   //mongoose schema
   var users= {};
   User.find({}, function (err, docs) {
    debugger;
   users = docs;          
   //process.send(users );               //Changed as in edit
    });

Here I am not getting any results in console. Can any one help me to get the details in child page from mongoDB and store it in one object. send those to mainjs to show in console

EDIT I have changed the process.send line to process.on method then it receiving in console but as a object.Even I tried with JSON.stringify(docs); also but same result.

 process.on('message', function(m) {
      process.send(users);
     });

IN Console

received: [object object]

Mongoose Schema

const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
    FirstName: String,
    LastName: String,
    Email: { type: String, unique: true }   

}, { versionKey: false });
Community
  • 1
  • 1
charan tej
  • 1,054
  • 10
  • 29

1 Answers1

0

This code snippet works perfectly for me. Please try this:

In main.js

var childprocess = require('child_process');
var child = childprocess.fork(__dirname + '/child');
child.on('message', function(m) {
    // Receive results from child process
    console.log('received: ',  m);
});

In child.js

var User = require('../models/User'); 
var users= {};
User.find({}, function (err, docs) {
    // Please add log in this line to check whether their is anything in database
    console.log(docs);
    debugger;
    users = docs;          
    process.send(users);              
});

For me this results:

enter image description here

Tolsee
  • 1,695
  • 14
  • 23
  • Thanks for answer. I tried it but I am receiving only `received:` In child.js Inside query process.send and console is not showing. – charan tej Apr 19 '17 at 14:36
  • @charantej It seems you donot have any problem in `child_process`. Can you provide code for your mongoose models and connection? – Tolsee Apr 19 '17 at 14:59
  • Does your database has any document? What I can guarantee you is, as per what you have shown here, there is no problem in your code. Might be other hidden issues. Because the code is perfectly running on my machine. And also mention your node version. – Tolsee Apr 20 '17 at 07:55
  • There are documents. What I have observed is Whenever I am writing process.send inside mongoose query it is not sending anything to main.js because even `received` also not showing in console and if I write outside it is showing what I initialised as `{}` – charan tej Apr 20 '17 at 08:00
  • Can u update the total file like mongoose connection and what are all remaining code or files in this sample project, because to check the with my project. – charan tej Apr 20 '17 at 12:22