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

亡梦爱人 提交于 2019-11-29 17:28:57

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!