How to fetch the details from mongo db and send or store in object in nodejs Fork method
Asked Answered
B

1

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 });
Boyt answered 19/4, 2017 at 8:19 Comment(6)
do you recieve any users from the db on the child process side?Guendolen
@JohannesMerz No I didnt receive, but that query when i am using in normal process it is workingBoyt
then its clearly sth with your mongoose setup, are you connecting to your db in the child process properly?Guendolen
@JohannesMerz Connection is working. Now I am receiving but in object. please see the edited question.Boyt
Please try console.log("Recieved", m) to log the result.Pardue
@Pardue Then is showing Received {} .Boyt
P
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

Pardue answered 19/4, 2017 at 14:15 Comment(5)
Thanks for answer. I tried it but I am receiving only received: In child.js Inside query process.send and console is not showing.Boyt
@charantej It seems you donot have any problem in child_process. Can you provide code for your mongoose models and connection?Pardue
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.Pardue
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 {}Boyt
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.Boyt

© 2022 - 2024 — McMap. All rights reserved.